Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Last revision Both sides next revision | ||
commands:builtin:caller [2012/01/15 09:09] ormaaj Some extra info off the top of my head - needs fact checking. |
commands:builtin:caller [2015/02/15 22:41] iankelling Make things a little clearer. |
||
---|---|---|---|
Line 15: | Line 15: | ||
The code below defines a function ''die'' that is used to exit the program. It prints a list of execution frames, starting with the topmost frame (0). The topmost frame is the "caller of the die function", in this case function "f1". | The code below defines a function ''die'' that is used to exit the program. It prints a list of execution frames, starting with the topmost frame (0). The topmost frame is the "caller of the die function", in this case function "f1". | ||
- | This way, you can print a kind of "stack trace" for debugging or logging purposes. | + | This way, you can print a "stack trace" for debugging or logging purposes. |
The code is made very simple, just to show the basic purposes. | The code is made very simple, just to show the basic purposes. | ||
Line 24: | Line 24: | ||
die() { | die() { | ||
local frame=0 | local frame=0 | ||
- | |||
while caller $frame; do | while caller $frame; do | ||
((frame++)); | ((frame++)); | ||
done | done | ||
- | |||
echo "$*" | echo "$*" | ||
exit 1 | exit 1 | ||
} | } | ||
- | f1() { | + | f1() { die "*** an error occured ***"; } |
- | die "*** an error occured ***" | + | f2() { f1; } |
- | } | + | f3() { f2; } |
- | + | ||
- | f2() { | + | |
- | f1 | + | |
- | } | + | |
- | + | ||
- | f3() { | + | |
- | f2 | + | |
- | } | + | |
f3 | f3 | ||
Line 50: | Line 40: | ||
**Output** | **Output** | ||
<code> | <code> | ||
- | 15 f1 ./callertest.sh | + | 12 f1 ./callertest.sh |
- | 19 f2 ./callertest.sh | + | 13 f2 ./callertest.sh |
- | 23 f3 ./callertest.sh | + | 14 f3 ./callertest.sh |
- | 26 main ./callertest.sh | + | 16 main ./callertest.sh |
*** an error occured *** | *** an error occured *** | ||
</code> | </code> |