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 [2021/01/09 14:39] – 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}" | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | 2 | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Check the return code ===== | ||
- | |||
- | <code bash> | ||
- | echo $? | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | 0 | ||
- | </ | ||
- | |||
- | <WRAP info> | ||
- | **NOTE: | ||
- | |||
- | A **1** indicates failure. | ||
- | |||
- | </ | ||
- | |||
- | |||
- | <WRAP box> | ||
- | **NOTE: | ||
- | |||
- | The **< | ||
- | |||
- | * 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 " | ||
- | |||
- | But if you run this script with **-e** or enable errexit: | ||
- | |||
- | <code bash> | ||
- | bash -e test.sh | ||
- | </ | ||
- | |||
- | then count++ is going to return 0 (post-increment) and the script will stop. | ||
- | |||
- | Checking the result: | ||
- | |||
- | <code bash> | ||
- | echo $? | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | 1 | ||
- | </ | ||
- | |||
- | This time a failure. | ||
- | |||
- | A definite trap to watch out for! | ||
- | </ | ||
bash/arithmetic_evaluation_and_errexit_trap.1610203191.txt.gz · Last modified: 2021/01/09 14:39 by peter