Skip to content

Instantly share code, notes, and snippets.

@bluegraybox
Created March 9, 2012 14:52
Show Gist options
  • Save bluegraybox/2006836 to your computer and use it in GitHub Desktop.
Save bluegraybox/2006836 to your computer and use it in GitHub Desktop.
Stack traces in bash
#!/bin/bash
function stacktrace() {
frame=0
while caller $frame ; do frame=$((frame + 1)) ; done
}
function lib_inner() {
stacktrace
}
function lib_outer() {
lib_inner
}
$ ./test.sh
9 lib_inner ./lib.sh
13 lib_outer ./lib.sh
8 inner ./test.sh
12 outer ./test.sh
15 main ./test.sh
function stacktrace() {
frame=0
while caller $frame ; do frame=$((frame + 1)) ; done
}
#!/bin/bash
dir=`dirname $0`
. $dir/lib.sh
function inner() {
lib_outer
}
function outer() {
inner
}
outer
@bluegraybox
Copy link
Author

This makes less sense in alphabetical order.

  • stacktrace.sh is the function to re-use
  • test.sh and lib.sh are test scripts; out.log shows what you see when you run test.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment