2010-01-18 54 views
0

好的,我无法从阅读Perl的文档中弄清楚这一点。我正在研究Apache的RHEL4初始化脚本......这行代码是做什么的?

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

为什么不只是httpd=/usr/sbin/httpd?怎么了所有额外的语法?

-Geoffrey Lee

+3

这不是Perl的,看起来像贝壳。 – Schwern 2010-01-18 06:46:47

+0

这将解释为什么Perl文档没有帮助! :P – geofflee 2010-01-18 12:22:36

回答

6

这不是Perl,它的外壳。 Init脚本通常用shell编写。具体来说,它的意思是“如果定义了,使用HTTPD环境变量,否则使用/ usr/sbin/httpd”。

查看here了解更多信息。

+0

你认为OP有错字错误吗?应该是$ HTTPD: - 而不仅仅是 - – ghostdog74 2010-01-18 07:29:47

+0

'$ {VAR-default}'起作用,尽管它不是记录的bash方式。我不能多说,我不是一个shell程序员。 – Schwern 2010-01-18 08:30:17

+0

它肯定有记录:查找手册页中的“Paramater Expansion”部分。 – 2010-01-18 14:41:24

2

冒号影响变量是否被检查为未设置或为空而不是仅检查是否未设置。

$ 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 

从Bash的手册页:

 
     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.