commands:builtin:caller

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
commands:builtin:caller [2010/09/22 05:51]
127.0.0.1 external edit
commands:builtin:caller [2015/02/15 22:41] (current)
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>​
 +
 +===== Notes =====
 +  * ''​caller''​ produces no output unless used within a script that's run from a real file. It isn't particularly useful for interactive use, but can be used to create a decent ''​die''​ function to track down errors in moderately complex scripts. <​code>​{ bash /dev/stdin; } <<<​$'​f(){ g; }\ng(){ h; }\nh(){ while caller $((n++)); do :; done; }\nf'</​code>​
 +  * For more sophisticated debugging, Bash extended debugging features are available and a number of special parameters that give more detail than caller (e.g. BASH_ARG{C,​V}). Tools such as [[ http://​bashdb.sourceforge.net/​|Bashdb ]] can assist in using some of Bash's more advanced debug features.
 +  * The Bash manpage and help text specifies that the argument to ''​caller''​ is an "​expr"​ (whatever that means). Only an integer is actually allowed, with no special interpretation of an "​expression"​ as far as we can tell.
  
 ===== Portability considerations ===== ===== Portability considerations =====
Line 62: Line 57:
  
 ===== See also ===== ===== See also =====
- 
  • commands/builtin/caller.1285134714.txt
  • Last modified: 2012/01/15 09:09
  • (external edit)