From dc3371f388f79adaddde8eff6ff0a286f8ee1127 Mon Sep 17 00:00:00 2001 From: pvincent Date: Sat, 11 Jul 2026 18:15:31 +0400 Subject: [PATCH] refactored1 --- bin/bash-back | 29 ++++++++++++++--------------- useless.sh | 13 +++++-------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/bin/bash-back b/bin/bash-back index 2735233..cf27b61 100755 --- a/bin/bash-back +++ b/bin/bash-back @@ -1,9 +1,8 @@ #!/usr/bin/env bash # CONSTANTS -TMP_ERROR=$(mktemp -t "$(basename $0).XXXXXXXX" --tmpdir=/run/user/$(id -u)) -DEBUG=${BASH_BACK_DEBUG:-false} -SCRIPT=$1 +BASH_BACK_ERROR=$(mktemp -t "$(basename $0).XXXXXXXX" --tmpdir=/run/user/$(id -u)) +BASH_BACK_DEBUG=${BASH_BACK_DEBUG:-false} # FUNCTIONS @@ -54,7 +53,7 @@ function on_exit { stack_summary=() if [[ $code -gt 0 ]]; then - last_error=$(tail -n1 $TMP_ERROR) + last_error=$(tail -n1 $BASH_BACK_ERROR) last_source=${stack_sources[0]} last_source=${last_source//./"\."} # replace . by \. regex1="^$last_source: line ([0-9]+): (.*)\$" @@ -97,7 +96,7 @@ function on_exit { } function dump_summary { - $DEBUG && >/dev/tty echo '---stacktrace---' + $BASH_BACK_DEBUG && >/dev/tty echo '---stacktrace---' for i in "${stack_summary[@]}"; do >&4 echo -e "$i" done @@ -105,13 +104,10 @@ function dump_summary { function dump_stderr { last_trim=${1:-false} - mapfile -t tmp_error <$TMP_ERROR - if $last_trim; then - unset tmp_error[-1] - tmp_error=("${tmp_error[@]}") - fi + mapfile -t tmp_error <$BASH_BACK_ERROR + $last_trim && unset tmp_error[-1] && tmp_error=("${tmp_error[@]}") if [[ "${#tmp_error[@]}" -gt 0 ]]; then - $DEBUG && >/dev/tty echo '---stderr---' + $BASH_BACK_DEBUG && >/dev/tty echo '---stderr---' for i in "${tmp_error[@]}"; do echo -e "$i" done @@ -126,17 +122,20 @@ function success { function cleanup { trap - ERR TERM INT EXIT exec 3>&- 4>&- - rm $TMP_ERROR + rm $BASH_BACK_ERROR } # MAIN set -TEue -o pipefail +[[ $# -ne 1 ]] && >&2 echo 'ERROR: script expected!' && builtin exit 1 + trap 'on_exit $?' ERR TERM INT EXIT trap 'on_return' DEBUG # magic FD -exec 3> >(tee $TMP_ERROR >/dev/null) 4>/dev/stderr -$DEBUG && >/dev/tty echo '---stdout---' -source $SCRIPT 2>&3 +exec 3> >(tee $BASH_BACK_ERROR >/dev/null) 4>/dev/stderr + +$BASH_BACK_DEBUG && >/dev/tty echo '---stdout---' +source $1 2>&3 success diff --git a/useless.sh b/useless.sh index e8dffa5..fa22621 100755 --- a/useless.sh +++ b/useless.sh @@ -22,6 +22,10 @@ function F1 { function main { F1 + F3 + >&2 echo some trace in stderr1 + lib/utility.bash + >&2 echo some trace in stderr2 } function F3 { @@ -30,13 +34,6 @@ function F3 { } echo start of useless -F3 ->&2 echo some trace in stderr1 -lib/utility.bash ->&2 echo some trace in stderr2 - -. lib/utility.bash - -main # error +main echo end of useless