From e2d3349d6f45102f23d2b093d3511e53b725309e Mon Sep 17 00:00:00 2001 From: pvincent Date: Tue, 7 Jul 2026 12:30:36 +0400 Subject: [PATCH] do not run twice --- lib/backtrace.bash | 20 ++++++++++++-------- test2.sh | 8 +++++--- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/backtrace.bash b/lib/backtrace.bash index a16dd40..7e20eb3 100644 --- a/lib/backtrace.bash +++ b/lib/backtrace.bash @@ -9,11 +9,15 @@ CURRENT_SOURCE="${BASH_SOURCE[0]}" function exit { local error_code stack_lines stack_functions stack_sources - error_code=$1 + error_code=${1:-0} stack_lines=("${BASH_LINENO[@]}") stack_functions=("${FUNCNAME[@]}") stack_sources=("${BASH_SOURCE[@]}") + + unset stack_functions[0] + unset stack_sources[0] + unset stack_lines[-1] # # # remove 1 on head list # [[ ${#stack_lines[@]} > 0 && ${stack_lines[0]} == 1 ]] && unset stack_lines[0] @@ -23,15 +27,15 @@ function exit { rm $script if [[ $error_code > 0 ]]; then - printf "\nEXIT #${error_code} due to error at line ${stack_lines[*]} : \n-----------------------------------------\n" + printf "\nEXIT #${error_code} due to error at line ${stack_lines} : \n-----------------------------------------\n" [[ -s /tmp/error ]] && echo -ne 'CAUSE:\t\t' && cat /tmp/error - echo -e "FUNCNAME:\t${FUNCNAME[@]}" >&2 # inner outer main - echo -e "BASH_SOURCE:\t${BASH_SOURCE[@]}" >&2 - echo -e "BASH_LINENO:\t${BASH_LINENO[@]}" >&2 + echo -e "FUNCNAME:\t${stack_functions[@]}" >&2 # inner outer main + echo -e "BASH_SOURCE:\t${stack_sources[@]}" >&2 + echo -e "BASH_LINENO:\t${stack_lines[@]}" >&2 trap - ERR TERM INT EXIT builtin exit "${error_code}" else - echo SUCCESS + echo __REAL_SUCCESS fi } @@ -47,8 +51,8 @@ function raise_error { _BACKTRACE=${_BACKTRACE:-false} # echo "_BACKTRACE=${_BACKTRACE}" -# a bit of magic: preserve performing the original script and rather running the backtrace version -${_BACKTRACE} && return +# a bit of magic: prevent performing the original script and rather running the backtrace version +${_BACKTRACE} && builtin exit set -TEue trap 'exit $?' ERR TERM INT EXIT diff --git a/test2.sh b/test2.sh index d34f654..017667a 100755 --- a/test2.sh +++ b/test2.sh @@ -7,12 +7,14 @@ function f1 { echo START f1 # return 4 # echo $nope - grep titi titi - grep titi titi 2>/dev/null - false + unknown_command + # grep titi titi + # grep titi titi 2>/dev/null + # false echo END F1 } echo START test2 +echo 'toto' >>/tmp/toto f1 echo END test2