[389a55]: trap_errors Maximize Restore History

Download this file

trap_errors    14 lines (13 with data), 621 Bytes

# Safe bash shell scripting practices
# This file should be sourced from a bash script, not run directly.
set -o errexit			# exit if anything fails
set -o errtrace			# shell functions inherit 'ERR' trap
init_cmd=$(basename $0)         # initial command
init_args="$@"                  # initial arguments to the script
function trap_handler() {
    local lastline="$1"               # line number of error occurence
    local errcode="$2"                # error code of last command
    echo "Error (${errcode}) at ${init_cmd}:${lastline},"\
        "arguments = ${init_args}" 1>&2
}
trap 'trap_handler ${LINENO} $?' ERR