Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Debugging bash:调试选项和函数_Debugging_Bash_Function_Unix_Ksh - Fatal编程技术网

Debugging bash:调试选项和函数

Debugging bash:调试选项和函数,debugging,bash,function,unix,ksh,Debugging,Bash,Function,Unix,Ksh,如果我跑 bash -x myscript.sh 我将获得调试输出 但是如果我在myscript.sh中有一个函数,那么函数中的代码不受-x选项的影响。它只将函数名写入输出 如何获得bash脚本中函数的调试输出 更新: 在ztank1013的响应之后,我才意识到我使用的是ksh,而不是bash。似乎bash在我的系统中默认启用了functrace选项(感谢bash-o-logist) 我很满意,但对于社区而言,我仍然向ksh提出这个问题 对于脚本: #!/bin/ksh a=2 tester

如果我跑

bash -x myscript.sh
我将获得调试输出

但是如果我在
myscript.sh
中有一个函数,那么函数中的代码不受-x选项的影响。它只将函数名写入输出

如何获得bash脚本中函数的调试输出

更新:

在ztank1013的响应之后,我才意识到我使用的是ksh,而不是bash。似乎bash在我的系统中默认启用了functrace选项(感谢bash-o-logist)

我很满意,但对于社区而言,我仍然向ksh提出这个问题

对于脚本:

#!/bin/ksh

a=2
testering(){
        a=3
        if [ $a -eq 3 ]; then
                echo lili
        fi
}
if [ $a -eq 2 ]; then
        echo mimi
fi

testering
exit
ksh-x./testdebug.sh的输出是:

+ a=2
+ [ 2 -eq 2 ]
+ echo mimi
mimi
+ testering
lili
+ exit
那么,对于ksh来说,诀窍是什么


(如果没有答案,“正确”将转到bash-o-logist。)

事实上,鉴于我的测试脚本(debug.sh),我无法重现您的问题:

我在关闭调试选项的情况下运行它:

[root ~]# ./debug.sh
HERE I am outside the function fun01() body!
BUT HERE I am inside the function fun01() body
并打开(bash-x…):

就我所见,fun01()函数中执行的那一行显示了一个start+,它是正在运行的调试器

@bash-o-logist即使我添加变量或if/then/else条件构造,我仍然会获得所有调试信息:

[root@ ~]# cat debug-new.sh
#!/bin/bash
fun01 () {
INSIDEVAR='Never really use this one'
echo "BUT HERE I am inside the function fun01() body"
if [ true ] ; then echo 'this is going to be printed always!' ; fi
}
echo "HERE I am outside the function fun01() body!"
sleep 2
fun01
exit
再次执行:

[root@ ~]# bash -x debug-new.sh
+ echo 'HERE I am outside the function fun01() body!'
HERE I am outside the function fun01() body!
+ sleep 2
+ fun01
+ INSIDEVAR='Never really use this one'
+ echo 'BUT HERE I am inside the function fun01() body'
BUT HERE I am inside the function fun01() body
+ '[' true ']'
+ echo 'this is going to be printed always!'
this is going to be printed always!
+ exit

使用bash,您可以在脚本中使用
functrace
选项

set -o functrace

请参阅手册页了解
bash
,了解其他调试器选项。

在ksh中使用
typeset-ft function name
跟踪函数

我有类似的问题,最后我为bash编写了自己的debbuger。试试看。。。我希望它能帮助您

这就是我在bash脚本中强制调试在功能块内部打开或关闭的方式

如果脚本启动时调试处于打开状态,则会在功能块内打开调试。每个功能块都有一个开始和结束消息,便于跟踪

这是用于运行时调试的。最好将输出重定向到日志文件,以便以后进行分析,例如

bash -x ./runtime_bash_debugging>log 2>&1
-- or --
./runtime_bash_debugging>log 2>&1
在开始时启用调试的示例输出

$ bash -x ./runtime_bash_debugging.sh
+ run_me_first
+ FN_NAME=run_me_first
+ MSG='BEGINNING OF: run_me_first'
+ test '' = on
++ date
+ echo 'I run first, it'\''s Sat Oct 27 19:11:06 MDT 2018'
I run first, it's Sat Oct 27 19:11:06 MDT 2018
+ MSG='END OF: run_me_first'
+ test '' = on
+ run_me_second
+ FN_NAME=run_me_second
+ MSG='BEGINNING OF: run_me_second'
+ test '' = on
+ echo 'I run second, my PID is 5744'
I run second, my PID is 5744
+ MSG='END OF: run_me_second'
+ test '' = on
+ echo Goodbye
Goodbye
+ exit
$ ./runtime_bash_debugging.sh
I run first, it's Sat Oct 27 19:11:09 MDT 2018
I run second, the PID is 4784
Goodbye
开始时调试关闭的示例输出

$ bash -x ./runtime_bash_debugging.sh
+ run_me_first
+ FN_NAME=run_me_first
+ MSG='BEGINNING OF: run_me_first'
+ test '' = on
++ date
+ echo 'I run first, it'\''s Sat Oct 27 19:11:06 MDT 2018'
I run first, it's Sat Oct 27 19:11:06 MDT 2018
+ MSG='END OF: run_me_first'
+ test '' = on
+ run_me_second
+ FN_NAME=run_me_second
+ MSG='BEGINNING OF: run_me_second'
+ test '' = on
+ echo 'I run second, my PID is 5744'
I run second, my PID is 5744
+ MSG='END OF: run_me_second'
+ test '' = on
+ echo Goodbye
Goodbye
+ exit
$ ./runtime_bash_debugging.sh
I run first, it's Sat Oct 27 19:11:09 MDT 2018
I run second, the PID is 4784
Goodbye
剧本

#!/bin/bash

# runtime bash debugging

fn_check_xtrace() {
    XTRACE_BEGIN_STATE=`set -o|awk '$1=="xtrace"{print $2}'`
    echo "${XTRACE_BEGIN_STATE}"
}


function run_me_first() {
    FN_NAME="run_me_first"
    MSG="BEGINNING OF: ${FN_NAME}"

    if test "${XTRACE_BEGIN_STATE}" = "on"
    then
    set -x
    fi

    echo "I run first, it's `date`"

    MSG="END OF: ${FN_NAME}"

    if test "${XTRACE_BEGIN_STATE}" = "on"
    then
    set -x
    fi
}


function run_me_second() {
    FN_NAME="run_me_second"
    MSG="BEGINNING OF: ${FN_NAME}"

    if test "${XTRACE_BEGIN_STATE}" = "on"
    then
    set -x
    fi

    echo "I run second, the PID is $$"

    MSG="END OF: ${FN_NAME}"

    if test "${XTRACE_BEGIN_STATE}" = "on"
    then
    set -x
    fi
}

run_me_first

run_me_second

echo "Goodbye"

exit

您不能只在函数中回显某些语句。尝试声明变量并在函数中使用if/else。@FlorinGhita在脚本中,将
函数名
替换为正在调试的函数名。屏幕截图看起来很有希望,但不起作用``1/usr/share/bdb$pwd/usr/share/bdb 1/usr/share/bdb$。/bdb.sh+用于inc/bdb.lang inc/bdb.vars inc/bdb.lib+INCFICH=/usr/share/bdb/inc/bdb.lang+字体-u INCUPR=inc/bdblang+INCFLAG=inc/bdblang/bdb.sh:line 29:INC/BDBLANG_INCLUDE:bad substitution+exit``我的西班牙语太糟糕了,无法理解你的代码。请详细说明启用functrace的功能。