You can use `set -e', check out BashFAQ 105 (link in my signature).
But, I would do something like:
Code:
command1 || exit 1
command4 || { echo "command4 failed" >&2; exit 4; }
If you want to use verbose error messages you could write a little function to keep your code clean and organized:
Code:
#!/bin/bash
err()
{
r="$1"
case "$r" in
1) printf '%s\n' "error 1" >&2 ;;
2) printf '%s\n' "error 2" >&2 ;;
4) printf '%s\n' "error 4" >&2 ;;
*) printf '%s\n' "unknown error" >&2 ; exit -1 ;;
esac
exit "$r"
}
command1 || err 1
command2 || err 2
command3 || err 4
exit 0
Bookmarks