-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbash-backtrace.sh
42 lines (31 loc) · 910 Bytes
/
bash-backtrace.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Add python-like backtraces to your bash shell scripts!
#
# Use like so:
#
# #!/bin/bash
# set -eu
# source "$password_vault/bin/lib/bash-backtrace.sh"
# ...
#
if [ -z "${BASH_VERSION-}" ]; then
echo >&2 "Error: this script only works in bash"
return 1 || exit 1
fi
set -E
# References:
# - https://gist.github.com/ryo1kato/3102982
# - https://gist.github.com/kergoth/6395873
bash_backtrace() {
local ret=$?
local frame
local FRAMES=${#BASH_SOURCE[@]}
echo >&2 "Traceback (most recent call last):"
for ((frame=FRAMES-2; frame >= 0; frame--)); do
local lineno=${BASH_LINENO[frame]}
printf >&2 ' File "%s", line %d, in %s\n' \
"${BASH_SOURCE[frame+1]}" "$lineno" "${FUNCNAME[frame+1]}"
sed >&2 -n "${lineno}s/^[ ]*/ /p" "${BASH_SOURCE[frame+1]}"
done
printf >&2 "Exiting with status %d\n" "$ret"
}
trap bash_backtrace ERR