次のような expr_sample0.sh があったとします。
expr "$1" + 1 > /dev/null if [ $? -le 1 ]; then echo "$1 is number" else echo "$1 is not number" fi
コマンドを実行するときにパラメータへ数字を渡すときはいいのですが、文字を渡してエラーとするときに、exprコマンドのエラーも一緒に表示されてしまいます。
$ sh expr_sample0.sh 1 1 is number $ sh expr_sample0.sh 2 2 is number $ sh expr_sample0.sh a expr: not a decimal number: 'a' a is not number
コマンドを実行した時に、コンソール画面へエラーが表示されると困るときもあるので、スクリプトで使っているコマンドでエラーが発生した場合は何も出力しないようにしたいときがあります。
続きを読む