Browse Source

return

main
pvincent 2 weeks ago
parent
commit
3fb0cf75bd
  1. 9
      _stderr
  2. 5
      _stdout
  3. 53
      bin/bash-back
  4. 1
      lib/utility.bash
  5. 12
      useless.sh

9
_stderr

@ -0,0 +1,9 @@
some trace in stderr
This is stderr
ERROR 10: <undefined>
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 <main>

5
_stdout

@ -0,0 +1,5 @@
start of useless
F3
F1
stdout A
IN blabla OUT

53
bin/bash-back

@ -7,12 +7,28 @@ SCRIPT=$1
# FUNCTIONS # FUNCTIONS
# overridden function which permits stacktracing of original `return` statement
function return { 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 { function exit {
code=${1:-0} code=${1:-0}
>&4 dump_stderr
stack_lines=("${BASH_LINENO[@]}") stack_lines=("${BASH_LINENO[@]}")
stack_functions=("${FUNCNAME[@]}") stack_functions=("${FUNCNAME[@]}")
@ -28,38 +44,51 @@ function exit {
stack_functions=("${stack_functions[@]}") stack_functions=("${stack_functions[@]}")
stack_sources=("${stack_sources[@]}") 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) last_error=$(tail -n1 $TMP_ERROR)
$DEBUG && >/dev/tty echo '---stderr---'
>&4 echo "ERROR $code: [$last_error]"
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: <undefined>"
fi fi
for i in "${!stack_functions[@]}"; do for i in "${!stack_functions[@]}"; do
>&4 printf "\t%30s\t%s\n" "${stack_sources[$i]}:${stack_lines[$i]}" "${stack_functions[$i]}" >&4 printf "\t%30s\t%s\n" "${stack_sources[$i]}:${stack_lines[$i]}" "${stack_functions[$i]}"
done done
fi
# echo "${stack_lines[@]}" # echo "${stack_lines[@]}"
# echo "${stack_functions[@]}" # echo "${stack_functions[@]}"
# echo "${stack_sources[@]}" # echo "${stack_sources[@]}"
cleanup ${code}
cleanup
builtin exit $code
} }
function success {
function dump_stderr {
if [[ -s $TMP_ERROR ]]; then if [[ -s $TMP_ERROR ]]; then
$DEBUG && >/dev/tty echo '---stderr---' $DEBUG && >/dev/tty echo '---stderr---'
>&2 cat $TMP_ERROR
cat $TMP_ERROR
fi fi
cleanup 0
}
function success {
>&2 dump_stderr
cleanup
} }
function cleanup { function cleanup {
code=${1:-0}
$DEBUG && >/dev/tty echo "---exit $1---"
trap - ERR TERM INT EXIT trap - ERR TERM INT EXIT
rm $TMP_ERROR
exec 3>&- 4>&- exec 3>&- 4>&-
builtin exit $code
rm $TMP_ERROR
} }
# MAIN # MAIN

1
lib/utility.bash

@ -4,6 +4,7 @@ function blabla {
} }
function suite { function suite {
exit 10
return 7 return 7
echo $nope echo $nope
command_not_found command_not_found

12
useless.sh

@ -4,10 +4,11 @@ function F2 {
echo "stdout A" echo "stdout A"
# echo toto >>/tmp/toto # echo toto >>/tmp/toto
>&2 echo "This is stderr" >&2 echo "This is stderr"
blabla blabla
$nope
exit 6 exit 6
grep titi toto grep titi toto
$nope
echo "stdout B" echo "stdout B"
echo "stdout C" echo "stdout C"
# command_not_found # command_not_found
@ -22,7 +23,14 @@ function main {
F1 F1
} }
. lib/utility.bash
function F3 {
echo F3
return 0
}
echo start of useless echo start of useless
F3
. lib/utility.bash
>&2 echo some trace in stderr
main main
echo end of useless echo end of useless
Loading…
Cancel
Save