私は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'
はい! manページから
:
${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に入れて実行すると、コンソールウィンドウに何も表示されません。 –
私は何か間違ったことがあったに違いない、それは実際に動作します –
btw、あなたの答えも大きかった!私は余分な括弧が好きです;) –
完璧!まさに私が探していたものです。 *その他の変数を使用する必要があります。 –
厳密に言うと、ここでは「エクスポート」は必要ありません。 –