4 ms·
There are quite a few examples around, just google the parameters 'set euo pipefail' (first hit [0]) you're right that just putting the set options on top of y
by y4mi 8y ago
There are quite a few examples around, just google the parameters 'set euo pipefail' (first hit [0])
you're right that just putting the set options on top of your file would be a bad idea. you need to write your script to actually account for these errors, making possible exitcodes entirely transparent
1 #!/bin/bash
2
3 set -euo pipefail
4 if ${DEBUG:-false}; then set -x; fi
5
6 function random_exitcode(){
7 return $(( RANDOM % 5))
8 }
9
10 function handle_error(){
11 case "$?" in
12 1) echo "handling exitcode 1!";;
13 2) echo "handling exitcode 2!!";;
14 3) echo "handling exitcode 3!!!";;
15 *) echo "encountered unexpected exitcode: $?"; exit 2;;
16 esac
17 }
18
19 random_exitcode || handle_error
or, if you don't like functions:
random_exitcode || LAST_EXITCODE=$?
case "${LAST_EXITCODE:=0}" in ...
[0] https://coderwall.com/p/fkfaqq/safer-bash-scripts-with-set-euxo-pipefail https://coderwall.com/p/fkfaqq/safer-bash-scripts-with-set-e...
- bloopernova 8y agoMany thanks, I very much appreciate the effort you put into your reply, and your followup.