4 ms·
Good info. You can improve your debugging by using exit codes like this: # die: print error message to stderr, then exit with error code. # example: di
by jph 3y ago
Good info. You can improve your debugging by using exit codes like this:
# die: print error message to stderr, then exit with error code.
# example: die 69 "Service unavailable."
die() {
n="$1" ; shift ; >&2 printf %s\\n "$*" ; exit "$n"
}
Many more shell script exit codes and helper functions:
https://github.com/SixArm/unix-shell-script-kit/blob/main/unix-shell-script-kit https://github.com/SixArm/unix-shell-script-kit/blob/main/un...
- ykonstant 3y agoThat's a nice list; I guess every experienced user has their helper functions. However, I have a small criticism for the philosophy of that `die`: `die` functions should pass by default the exit code of the failed command, and not silence its error output. If I want to give my own meaning to the command failure in a large script for instance, I will use a different, more specialized `die`. My own die is roughly as follows: __errex() { printf 'Fatal error [%s] on line %s in '"'"'%s'"'"': %s\n' \ "${1:-"?"}" \ "${2:-"?"}" \ "${3:-"unknown script"}" \ "${4:-"unknown error"}" >&2 ; exit "${1:-1}" } alias die='__errex "$?" "${LINENO}" "$0"'