bash:arithmetic_evaluation_and_errexit_trap
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
bash:arithmetic_evaluation_and_errexit_trap [2016/11/25 16:08] – peter | bash:arithmetic_evaluation_and_errexit_trap [2021/01/26 12:00] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Bash - Arithmetic evaluation and errexit trap ====== | ||
- | |||
- | <code bash> | ||
- | count=0 | ||
- | things=" | ||
- | |||
- | for i in $things; | ||
- | do | ||
- | if [ $i == " | ||
- | (( count++ )) | ||
- | fi | ||
- | done | ||
- | |||
- | echo "Count is ${count}" | ||
- | </ | ||
- | |||
- | Looks fine? I've probably written this many times. There' | ||
- | |||
- | <WRAP box> | ||
- | < | ||
- | The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let " | ||
- | </ | ||
- | |||
- | When you run this script with -e or enable errexit -- probably because the script has become too big to be reliable without it -- count++ is going to return 0 (post-increment) and per above stop the script. A definite trap to watch out for! | ||
bash/arithmetic_evaluation_and_errexit_trap.1480090102.txt.gz · Last modified: 2020/07/15 09:30 (external edit)