From 3fb0cf75bdffef7ddf56ba1be49e7780705430f0 Mon Sep 17 00:00:00 2001 From: pvincent Date: Fri, 10 Jul 2026 17:21:21 +0400 Subject: [PATCH] return --- _stderr | 9 +++++++ _stdout | 5 ++++ bin/bash-back | 61 +++++++++++++++++++++++++++++++++++------------- lib/utility.bash | 1 + useless.sh | 12 ++++++++-- 5 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 _stderr create mode 100644 _stdout diff --git a/_stderr b/_stderr new file mode 100644 index 0000000..93392d9 --- /dev/null +++ b/_stderr @@ -0,0 +1,9 @@ +some trace in stderr +This is stderr +ERROR 10: + lib/utility.bash:7 suite + lib/utility.bash:3 blabla + useless.sh:8 F2 + useless.sh:19 F1 + useless.sh:23 main + useless.sh:35
diff --git a/_stdout b/_stdout new file mode 100644 index 0000000..0c1aa15 --- /dev/null +++ b/_stdout @@ -0,0 +1,5 @@ +start of useless +F3 +F1 +stdout A +IN blabla OUT diff --git a/bin/bash-back b/bin/bash-back index 08618b3..3055e52 100755 --- a/bin/bash-back +++ b/bin/bash-back @@ -7,12 +7,28 @@ SCRIPT=$1 # FUNCTIONS +# overridden function which permits stacktracing of original `return` statement function return { - builtin return ${1:-} + code=${1:-0} + [[ $code -gt 0 ]] && >&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: return $@" + builtin return $code } +# # overridden function which permits stacktracing of original `return` statement +# function exit { +# code=${1:-0} +# if [[ $code -gt 0 ]]; then +# >/dev/stderr echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: exit $@" +# on_exit $code +# builtin exit $code +# else +# success +# fi +# } + function exit { code=${1:-0} + >&4 dump_stderr stack_lines=("${BASH_LINENO[@]}") stack_functions=("${FUNCNAME[@]}") @@ -28,38 +44,51 @@ function exit { stack_functions=("${stack_functions[@]}") stack_sources=("${stack_sources[@]}") - if [[ -s $TMP_ERROR ]]; then + if [[ $code -gt 0 ]]; then + $DEBUG && >/dev/tty echo '---stacktrace---' last_error=$(tail -n1 $TMP_ERROR) - $DEBUG && >/dev/tty echo '---stderr---' - >&4 echo "ERROR $code: [$last_error]" - fi + last_source=${stack_sources[0]} + last_source=${last_source//./"\."} # replace . by \. + regex="^$last_source: line ([0-9]+): (.*)$" + if [[ "$last_error" =~ $regex ]]; then + line=${BASH_REMATCH[1]} + message=${BASH_REMATCH[2]} + regex=': unbound variable$' + [[ $code == 1 ]] && [[ "$message" =~ $regex ]] && stack_lines[0]=$line + >&4 echo -e "ERROR $code: [$message]" + else + >&4 echo "ERROR $code: " + fi - for i in "${!stack_functions[@]}"; do - >&4 printf "\t%30s\t%s\n" "${stack_sources[$i]}:${stack_lines[$i]}" "${stack_functions[$i]}" - done + for i in "${!stack_functions[@]}"; do + >&4 printf "\t%30s\t%s\n" "${stack_sources[$i]}:${stack_lines[$i]}" "${stack_functions[$i]}" + done + fi # echo "${stack_lines[@]}" # echo "${stack_functions[@]}" # echo "${stack_sources[@]}" - cleanup ${code} + cleanup + builtin exit $code } -function success { +function dump_stderr { if [[ -s $TMP_ERROR ]]; then $DEBUG && >/dev/tty echo '---stderr---' - >&2 cat $TMP_ERROR + cat $TMP_ERROR fi - cleanup 0 +} + +function success { + >&2 dump_stderr + cleanup } function cleanup { - code=${1:-0} - $DEBUG && >/dev/tty echo "---exit $1---" trap - ERR TERM INT EXIT - rm $TMP_ERROR exec 3>&- 4>&- - builtin exit $code + rm $TMP_ERROR } # MAIN diff --git a/lib/utility.bash b/lib/utility.bash index 9be6d77..c8bbff5 100644 --- a/lib/utility.bash +++ b/lib/utility.bash @@ -4,6 +4,7 @@ function blabla { } function suite { + exit 10 return 7 echo $nope command_not_found diff --git a/useless.sh b/useless.sh index 6c96c3d..9d310c2 100755 --- a/useless.sh +++ b/useless.sh @@ -4,10 +4,11 @@ function F2 { echo "stdout A" # echo toto >>/tmp/toto >&2 echo "This is stderr" + blabla + $nope exit 6 grep titi toto - $nope echo "stdout B" echo "stdout C" # command_not_found @@ -22,7 +23,14 @@ function main { F1 } -. lib/utility.bash +function F3 { + echo F3 + return 0 +} + echo start of useless +F3 +. lib/utility.bash +>&2 echo some trace in stderr main echo end of useless