-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexception_handling.sh
More file actions
executable file
·47 lines (39 loc) · 903 Bytes
/
exception_handling.sh
File metadata and controls
executable file
·47 lines (39 loc) · 903 Bytes
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
43
44
45
46
47
#!/bin/bash
set -E
trap 'catch $?' ERR EXIT
catch() {
#trap '-' ERR EXIT
if [ "$1" == 0 ]; then
return
fi
# Print stack trace
echo "Caught error $1 in:" >&2
frame=0
line="$(caller $frame 2>&1 | cut -d ' ' -f 1)"
while [ -n "$line" ]; do
subroutine="$(caller $frame 2>&1 | cut -d ' ' -f 2)"
file="$(caller $frame 2>&1 | cut -d ' ' -f 3)"
echo "From $file:$line in $subroutine" >&2
echo " $(sed -n ${line}p $file)" >&2
((frame++)) || true
line="$(caller $frame 2>&1 | cut -d ' ' -f 1)"
done
echo >&2
# Put cleanup code here
#exit $1
}
if [ "${BASH_SOURCE}" = "${0}" ]; then
something_else() {
echo "foobar"
false
}
do_something() {
false
echo "something"
something_else
}
echo "begin"
false
do_something
echo "end"
fi