Trace of executed programs called by a Bash script

后端 未结 8 1857
难免孤独
难免孤独 2020-12-13 14:44

A script is misbehaving. I need to know who calls that script, and who calls the calling script, and so on, only by modifying the misbehaving script.

This is similar

8条回答
  •  既然无缘
    2020-12-13 15:04

    You can use Bash Debugger http://bashdb.sourceforge.net/

    Or, as mentioned in the previous comments, the caller bash built-in. See: http://wiki.bash-hackers.org/commands/builtin/caller

    i=0; while caller $i ;do ((i++)) ;done
    

    Or as a bash function:

    dump_stack(){
        local i=0
        local line_no
        local function_name
        local file_name
        while caller $i ;do ((i++)) ;done | while read line_no function_name file_name;do echo -e "\t$file_name:$line_no\t$function_name" ;done >&2
    }
    

    Another way to do it is to change PS4 and enable xtrace:

    PS4='+$(date "+%F %T") ${FUNCNAME[0]}() $BASH_SOURCE:${BASH_LINENO[0]}+ '
    set -o xtrace    # Comment this line to disable tracing.
    

提交回复
热议问题