3 ms·
You should make a habit of setting set -euo Pipefail if "${DEBUG:-false}”; then set -x; fi The -o Pipefail is irrelevant in this case, as you're not using
by y4mi 8y ago
You should make a habit of setting
set -euo Pipefail
if "${DEBUG:-false}”; then set -x; fi
The -o Pipefail is irrelevant in this case, as you're not using any.. but better it's there in case you ever extend the script.
As others have pointed out: readability comes right after correctness in shell script, do keeping it simple as you have done is always a good idea.
- bloopernova 8y agoI hope you see this reply, I am sorry for it being so late: Do you have a link to a good explanation of why you'd set those? The "set -e" seems to indicate that the script would fail immediately if the borg backup job fails, which would prevent the Zabbix "send" command from alerting the monitoring system. Thank you for your feedback, it's given me more to learn and think about!
- y4mi 8y agoThere 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.