L_trap¶
Trap and other related functions.
The most important and overused function is here L_print_traceback
. It print the full traceback with the source code locations. This is one of the features I missed in Bash a lot. Where did the error happen?
When . L_lib.sh
is sourced without -n
argument, it executes L_trap_err_init
. If set -e
is set and trap ERR
is empty, it will execute the function L_trap_err_enable
to set the trap on ERR to print the traceback if a command fails.
You can also trap 'L_trap_err $?' EXIT
to print the error code and the last command that failed. This is useful for debugging.
It is all built around L_print_traceback
function which prints the whole bash traceback in a python-like format style.
Then there are L_trap_push
and L_trap_pop
functions to push and pop another code block with a newline to the trap handler. This is a work in progress, as it would be amazing to handle $?
correctly.
trap
¶
L_print_traceback
¶
Prints traceback
:
Example traceback:
Traceback from pid 3973390 (most recent call last):
File ./bin/L_lib.sh, line 2921, in main()
2921 >> _L_lib_main "$@"
File ./bin/L_lib.sh, line 2912, in _L_lib_main()
2912 >> "test") _L_lib_run_tests "$@"; ;;
File ./bin/L_lib.sh, line 2793, in _L_lib_run_tests()
2793 >> "$_L_test"
File ./bin/L_lib.sh, line 891, in _L_test_other()
891 >> L_unittest_eq "$max" 4
File ./bin/L_lib.sh, line 1412, in L_unittest_eq()
1412 >> _L_unittest_showdiff "$1" "$2"
File ./bin/L_lib.sh, line 1391, in _L_unittest_showdiff()
1391 >> sdiff <(cat <<<"$1") - <<<"$2"
Arguments:
-
[$1]
int stack offset to start from (default: 0) -
[$2]
int number of lines to show around the line (default: 2)
L_trap_err_small
¶
Callback to be exectued on ERR trap that prints just the caller.
Example
trap 'L_trap_err_small' ERR
L_trap_err
¶
description Callback to be exectued on ERR trap that prints a traceback and exits.
Example
trap 'L_trap_err $?' ERR
trap 'L_trap_err $?' EXIT
Argument:
$1
int exit code
L_trap_err_enable
¶
Enable ERR trap with L_trap_err as callback
set -eEo functrace and register trap 'L_trap_err $?' ERR.
Example
L_trap_err_enable
L_trap_err_disable
¶
Disable ERR trap
Example
L_trap_err_disable
L_trap_err_init
¶
If set -e is set and ERR trap is not set, enable ERR trap with L_trap_err as callback
Example
L_trap_err_init
$L_TRAP_RETURN_RESTORE_SHOPT
¶
Create a return trap that restores all shopt options.
Example
func() {
eval "$L_TRAP_RETURN_RESTORE_SHOPT"
shopt -s extglob
# stuff with extglob
}
L_trap_to_number
¶
Convert trap name to number
Option:
-v <var>
var
Argument:
$1
trap name or trap number
L_trap_to_number_v
¶
L_trap_to_name
¶
convert trap number to trap name
Example
L_trap_to_name -v var 0 && L_assert '' test "$var" = EXIT
Option:
-v <var>
var
Argument:
$1
signal name or signal number
L_trap_to_name_v
¶
L_trap_get
¶
Get the current value of trap
Example
trap 'echo hi' EXIT
L_trap_get -v var EXIT
L_assert '' test "$var" = 'echo hi'
Option:
-v <var>
var
Argument:
$1
L_trap_get_v
¶
L_trap_push
¶
Suffix a newline and the command to the trap value
Arguments:
-
$1
str command to execute -
$2
str signal to handle
L_trap_pop
¶
remove a command from trap up until the last newline
Argument:
$1
str signal to handle
Shellcheck disable= SC2064