* testコマンド 論理演算子 AND OR NOT [#h49baca7]

*** AMD [#eceba415]
*** AND [#eceba415]

 n1=10; n2=7
 if [ "$n1" -gt 5 -a "$n2" -gt 5 ]; then echo OK; fi
 
*** OR [#b7c49bff]

 n1=5; n2=10
 if [ "$n1" -gt 7 -o "$n2" -gt 7 ]; then echo OK; fi

*** 複雑な組み合わせ [#l7a6afbf]

 n1=1; n2=10; n3=10
 if [ \( "$n1" -gt 5 -a "$n2" -gt 5 \) -o "$n3" -gt 5 ]; then echo OK; fi

複雑な条件式の場合、複合コマンド[[]]を利用するのが無難。上のような通常のテストコマンドではn1が未定義などの場合、エラーが発生するので。

** NOT [#de2f3e67]

 if [ ! -f /tmp/1.txt -a ! -f /tmp/2.txt ]; then
   echo "no 1.txt, no 2.txt"
 fi

** 参考 [#r60c0c21]
- http://www.ibm.com/developerworks/jp/linux/library/l-bash-test.html
- http://lagendra.s.kanazawa-u.ac.jp/ogurisu/manuals/sh-text/sh/node48.html



トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS