Skip to content
Advertisement

Bash debug calling stack

I’m using a generic procedure to trap and describe the error or abnormal situations, instead of the usual ‘2>…’ error construct.

The idea will be to have a procedure like this simplified version:

function debug(){
    echo "Fatal Error: $PWD:$BASH_SOURCE:$LINENO $*"
    ....
    exit 1
}

and then, used as in this example:

[ -z "$PARAMETER" ] && debug The parameter was not provided

The issues are:

  • BASH_SOURCE is the running source. The idea is to show the calling source, if the procedure ‘debug’ is global.
  • LINENO is the line where the expansion is executed and no the calling address.

Note: BASH_SOURCE[0] and BASH_SOURCE[1] provide ‘some help’ when the procedure is sourced.

This will be used to notify ‘user’ errors, on a centralized error message procedure. This may include a post in syslog and on other log files. However, some messages may look alike and knowing where in the source code the error was detected, help the developers.

Is there any method to obtain a calling stack on bash?

Advertisement

Answer

You can use bash built-in command “caller” for this. Link –> http://wiki.bash-hackers.org/commands/builtin/caller

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement