Last active
October 18, 2016 05:26
-
-
Save lexton/aa0ab5e8f4b5c3e6da2108274da8e4f2 to your computer and use it in GitHub Desktop.
Printing a stack trace in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
fatal() { | |
local frame=0 | |
while caller $frame; do | |
((frame++)); | |
done | |
echo "$*" | |
exit 1 | |
} | |
f1(){ | |
undefined_command | |
if [ $? -ne 0 ]; then | |
fatal "things went bad." | |
fi | |
} | |
f2(){ | |
f1 | |
} | |
f3(){ | |
f2 | |
} | |
f3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment