开发者

${HTTPD-...} Perl syntax

开发者 https://www.devze.com 2022-12-16 13:25 出处:网络
Ok, I can\'t figure this out from reading Perl\'s documentation. I\'m looking at the RHEL4 init script for开发者_开发百科 Apache... What does this line of code do?

Ok, I can't figure this out from reading Perl's documentation. I'm looking at the RHEL4 init script for开发者_开发百科 Apache... What does this line of code do?

httpd=${HTTPD-/usr/sbin/httpd}

Why not just httpd=/usr/sbin/httpd? What's up with all the extra syntax?

-Geoffrey Lee


That's not Perl, its shell. Init scripts are usually written in shell. Specifically it means "if defined, use the HTTPD environment variable, otherwise use /usr/sbin/httpd".

Look here for more info.


The colon affects whether the variable is checked for being unset or null versus only checking for whether it's unset.

$ var="goodbye"; echo ${var-hello}
goodbye
$ var="goodbye"; echo ${var:-hello}
goodbye
$ var= ; echo ${var:-hello}
hello
$ var= ; echo ${var-hello}    # var is null, only test for unset so no sub. made

$ unset var; echo ${var:-hello}
hello
$ unset var; echo ${var-hello}
hello

From the Bash man page:

       When not performing substring expansion,  using  the  forms  documented
       below,  bash tests for a parameter that is unset or null.  Omitting the
       colon results in a test only for a parameter that is unset.

       ${parameter:-word}
              Use Default Values.  If parameter is unset or null,  the  expan‐
              sion  of word is substituted.  Otherwise, the value of parameter
              is substituted.
0

精彩评论

暂无评论...
验证码 换一张
取 消