开发者

${HTTPD-...} Perl syntax

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜