2010-01-18 55 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.