Browse Source

do not run twice

main
pvincent 2 weeks ago
parent
commit
e2d3349d6f
  1. 20
      lib/backtrace.bash
  2. 8
      test2.sh

20
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

8
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
Loading…
Cancel
Save