LoginSignup
9
9

More than 5 years have passed since last update.

exprは計算に成功してもリターンコードが0とは限らない

Posted at

シェルスクリプトで整数計算を行うコマンド"expr"を使うことがあるが、
exprは計算の結果が0の場合、"0=偽"と判定するため、リターンコードとして0以外を返す。
このため"set -e"とexprでのカウントダウンを併用していると問題になる場合がある。
例えば、以下のようなケース

loop.sh
#!/bin/sh
set -e
i=5
while [ $i -ge 1 ]
do
  echo now $i
  i=`expr $i - 1`
  echo result $i
done

最後の"result 0"が出力される前に、エラーが発生したとして中断される。

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9