if 1
1 2 3 4 5 6 |
# Error, 1 is not a command #if 1 ; then # echo "if 1 (${LINENO})" is true #else # echo "if 1 (${LINENO})" is false #fi |
if x
1 2 3 4 5 6 |
# Error, x is not a command #if x ; then # echo "if x (${LINENO})" is true #else # echo "if x (${LINENO})" is false #fi |
ifの次にはコマンドを書かなければならない。
if uname
1 2 3 4 5 6 |
# normal command, returns 0 when success, it means true if uname ; then echo "uname is true (${LINENO})" # COMES HERE else echo "uname is false (${LINENO})" fi |
uname -b
1 2 3 4 5 6 |
# illegal, returns 1, it means false if uname -b; then echo "uname -b is true (${LINENO})" else echo "uname -b is false (${LINENO})" # COMES HERE fi |
if $(command)
1 2 3 4 5 6 7 |
# Do with subshell # Bash error $(..) is not a command #if $(uname) ; then # echo "uname is true (${LINENO})" #else # echo "uname is false (${LINENO})" #fi |
if [ $(command) ]
1 2 3 4 5 6 |
echo '===Do with subshell===' if [ $(uname) ]; then echo "uname is true (${LINENO})" # COMES HERE else echo "uname is false (${LINENO})" fi |
if [ $(command -b) ]
1 2 3 4 5 |
if [ $(uname -b) ]; then echo "uname -b is true (${LINENO})" else echo "uname -b is false (${LINENO})" # COMES HERE fi |
問題:システムの種類によって処理を分ける
システムがMingかどうかを判定するif文
1 |
if uname | grep -q ^MINGW ; then |
別回答
1 |
if [[ $(uname | grep ^MINGW) ]]; then |
ここでは[と[[の違いは考慮してないが同じだと思う。[は古い形式のただのコマンド。[[はバッシュの組み込みだと思う。[[のほうがいいといわれている。
ダメな回答
1 |
if [[ $(uname | grep -q ^MINGW) ]]; then |