From a62bd7acc5c015f96b208ed44ba304e071579c9d Mon Sep 17 00:00:00 2001 From: pvincent Date: Mon, 6 Jul 2026 18:37:35 +0400 Subject: [PATCH] backtrace.bash --- install.sh | 48 ++++++---------------------------------------- lib/backtrace.bash | 47 +++++++++++++++++++++++++++++++++++++++++++++ test.sh | 12 ++++++++++++ 3 files changed, 65 insertions(+), 42 deletions(-) create mode 100755 test.sh diff --git a/install.sh b/install.sh index bb31f06..e2fa5e4 100755 --- a/install.sh +++ b/install.sh @@ -6,33 +6,6 @@ BASEDIR=$(dirname $0) ## FUNCTIONS -function exit { - on_exit $1 true -} - -function on_exit { - local error_code bypass_last_call stack_lines - error_code=$1 - - bypass_last_call=${2:-false} - stack_lines=("${BASH_LINENO[@]}") - $bypass_last_call && stack_lines=("${stack_lines[@]:1}") - - # remove 1 on head list - [[ ${#stack_lines[@]} > 0 && ${stack_lines[0]} == 1 ]] && unset stack_lines[0] - - # remove 0 on tail list - [[ ${#stack_lines[@]} > 0 && ${stack_lines[-1]} == 0 ]] && unset stack_lines[-1] - - if [[ $error_code > 0 ]]; then - printf "\nEXIT #${error_code} due to error at line ${stack_lines[*]} : \n-----------------------------------------\n" - trap - ERR TERM INT EXIT - builtin exit "${error_code}" - else - echo SUCCESS - fi -} - function assert_debian13 { test -f /etc/debian_version && grep --quiet ^13\. /etc/debian_version || @@ -67,26 +40,19 @@ function install_ghostty { function install_homebrew { # $nope + grep OK file # exit 11 # echo AAA + + on_exit 45 + exit 45 + raise_error 'BBB' false echo BBB bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" } -function raise_error { - message=${1:-an undefined error has occured!} - code=${2:-1} - echo "error ${code}: ${message}" >&2 - echo "FUNCNAME: ${FUNCNAME[@]}" >&2 # inner outer main - echo "BASH_SOURCE: ${BASH_SOURCE[@]}" script.sh script.sh script.sh >&2 - echo "BASH_LINENO: ${BASH_LINENO[@]}" 52 55 0 >&2 - trap - ERR TERM INT EXIT - # builtin exit "${code}" - on_exit $code true -} - function set_alternative { local key command path_to_command path_to_key key=$1 @@ -145,13 +111,11 @@ function install_neovim { else echo 'nvim-vanilla ready!' fi - exit 3 } ## MAIN -set -TEue -trap 'on_exit $?' ERR TERM INT EXIT +source "$BASEDIR/lib/backtrace.bash" assert_debian13 assert_gnome_desktop diff --git a/lib/backtrace.bash b/lib/backtrace.bash index e69de29..7701df9 100644 --- a/lib/backtrace.bash +++ b/lib/backtrace.bash @@ -0,0 +1,47 @@ +## CONSTANTS + +CURRENT_SOURCE="${BASH_SOURCE[0]}" + +## FUNCTIONS + +function exit { + on_exit $1 true +} + +function on_exit { + local error_code bypass_last_call stack_lines + error_code=$1 + + bypass_last_call=${2:-false} + echo $CURRENT_SOURCE bypass_last_call=$bypass_last_call + stack_lines=("${BASH_LINENO[@]}") + + # + # # remove 1 on head list + # [[ ${#stack_lines[@]} > 0 && ${stack_lines[0]} == 1 ]] && unset stack_lines[0] + # + # # remove 0 on tail list + # [[ ${#stack_lines[@]} > 0 && ${stack_lines[-1]} == 0 ]] && unset stack_lines[-1] + + if [[ $error_code > 0 ]]; then + printf "\nEXIT #${error_code} due to error at line ${stack_lines[*]} : \n-----------------------------------------\n" + echo "FUNCNAME: ${FUNCNAME[@]}" >&2 # inner outer main + echo "BASH_SOURCE: ${BASH_SOURCE[@]}" >&2 + echo "BASH_LINENO: ${BASH_LINENO[@]}" >&2 + trap - ERR TERM INT EXIT + builtin exit "${error_code}" + else + echo SUCCESS + fi +} + +function raise_error { + message=${1:-an undefined error has occured!} + code=${2:-1} + echo "error ${code}: ${message}" >&2 + trap - ERR TERM INT EXIT + on_exit $code true +} + +set -TEue +trap 'on_exit $?' ERR TERM INT EXIT diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..3588c6b --- /dev/null +++ b/test.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env -S -- bash + +## CONSTANTS + +BASEDIR=$(dirname $0) + +## MAIN + +source "$BASEDIR/lib/backtrace.bash" +echo START test +grep toto nope +echo END test