#!/usr/bin/env -S -- bash # CONSTANTS TMP_ERROR=$(mktemp -t "$(basename $0).XXXXXXXX" --tmpdir=/run/user/$(id -u)) # FUNCTIONS function exit { code=${1:-0} echo echo "ERROR #${code} due to:" stack_lines=("${BASH_LINENO[@]}") stack_functions=("${FUNCNAME[@]}") stack_sources=("${BASH_SOURCE[@]}") tmp_error=${2:-/dev/null} [[ -s $tmp_error ]] && cat $tmp_error && rm $tmp_error echo "${stack_lines[@]}" echo "${stack_functions[@]}" echo "${stack_sources[@]}" trap - ERR TERM INT EXIT builtin exit $code } # MAIN opt_debug="" [[ $1 == '-x' || $1 == '--debug' ]] && opt_debug="-x" && shift command=$1 shift original_args=("$@") quoted_args=() for i in "${original_args[@]}"; do quoted_args+=("${i@Q}"); done set -TEue -o pipefail export -f exit # echo PROCESS START bash $opt_debug -c " set -TEue -o pipefail; trap 'exit \$? $TMP_ERROR' ERR TERM INT EXIT; source $command ${quoted_args[*]} " 3>&1 1>&2 2>&3 | tee $TMP_ERROR # TODO: delay the stacktrace message to output to stderr, idea: $TMP_ERROR + $TMP_STACK # echo PROCESS END succesfully