Friday, September 14, 2012
10:00 PM

Bash Script: Get the Information about the caller of the function (backtrace)

Caller Returns  the  context  of  any active subroutine call (a shell function or a script executed with the . or source builtins). caller displays the line number and source filename of the current subroutine call.  If a non-negative integer is supplied as expr,  caller displays  the line number, subroutine name, and source file corresponding to that position in the current execution call stack.  This extra information may be used, for example, to print a stack trace. 

Below is simple bash script which demonstrate the usage of caller ...
feel free to copy and use this code

Source: cat caller.sh
#!/bin/bash

foo() {
        caller 0
        echo "In function: foo"
}

echo "Outside of the function"

goo() {
        echo "In function: goo"
        foo
}

goo

Output: ./caller.sh
Outside of the function
In function: goo
13 goo ./caller.sh
In function: foo

0 comments:

Post a Comment