我是bash的新手,但已經做了很多PHP和Javascript。BASH:它是否支持條件變量,如var =「test」?「1」:「2」
是否有某種等同於此PHP?
$default = 10;
$var = (!$var) ? $default : $var;
感謝
我是bash的新手,但已經做了很多PHP和Javascript。BASH:它是否支持條件變量,如var =「test」?「1」:「2」
是否有某種等同於此PHP?
$default = 10;
$var = (!$var) ? $default : $var;
感謝
是的,它的作用:
var=${var:-10}
即使其他變量:
unset var
export def=99
echo ${var:-${def}} # gives '99'
export var=7
echo ${var:-${def}} # gives '7'
是的!
從手冊頁:
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word
is substituted. Otherwise, the value of parameter is substituted.
${parameter:=word}
Assign Default Values. If parameter is unset or null, the expansion of word
is assigned to parameter. The value of parameter is then substituted.
Positional parameters and special parameters may not be assigned to in this way.
${parameter:?word}
Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to
that effect if word is not present) is written to the standard error and the shell, if it is not inter‐
active, exits. Otherwise, the value of parameter is substituted.
${parameter:+word}
Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of
word is substituted.
把這個:http://bash.pastebin.com/f4e14cd53放入一個文件並執行它迴應控制檯窗口什麼也沒有。 – 2009-09-25 04:24:53
我一定是做錯了,那實際上工作 – 2009-09-25 04:35:48
順便說一句,你的答案也很棒!我只是喜歡額外的括號;) – 2009-09-25 04:57:51
完美!正是我所期待的。肯定需要使用* other *變量。 – 2009-09-25 04:53:06
嚴格來說,這裏不需要'export'。 – 2009-09-25 09:13:02