bash:调试选项和函数
如果我运行
bash -x myscript.sh
我会得到调试输出。
但是,如果我在 myscript.sh
中有一个函数,则该函数中的代码不受 -x 选项的影响。它仅写入输出函数的名称。
如何获取 bash 脚本中函数的调试输出?
更新:
根据ztank1013的回复,我刚刚意识到我使用的是ksh,而不是bash。看来 bash 默认情况下在我的系统中启用了 functrace 选项(感谢 bash-o-logger),
我很满意,但对于社区来说,我仍然对 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。)
If I run
bash -x myscript.sh
I'll get debugging output.
But if I have a function in myscript.sh
, the code in the function is immune to -x option. It writes to output only the name of the function.
How to obtain debugging output for functions in bash scripts?
Update:
Following the ztank1013's response, I just realized that I used ksh, not bash. Seems bash has by default the functrace option enabled in my system(thanks bash-o-logist)
I am satisfied, but for the community I maintain the question open for ksh.
For script:
#!/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
output of ksh -x ./testdebug.sh
is:
+ a=2
+ [ 2 -eq 2 ]
+ echo mimi
mimi
+ testering
lili
+ exit
So, for ksh, what's the trick?
(If no answer will come, the 'correct' will go to bash-o-logist.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 bash,您可以在脚本中使用
functrace
选项,请参阅
bash
的联机帮助页了解其他调试器选项。With bash, you can use
functrace
option in your scriptSee manpage for
bash
for other debugger options.我无法重现您的问题,事实上给定我的测试脚本(debug.sh):
我在关闭调试选项的情况下运行它:
并打开(bash -x ...):
据我所知,在fun01() 函数以起始 + 显示,这是正在运行的调试器。
@bash-o-logger即使我添加变量或 if/then/else 条件构造,我仍然可以获得所有调试信息:
再次执行:
I cannot reproduce your problem, in fact given my test script (debug.sh):
I run it with debug option turned off:
and turned on (bash -x ...):
As far as I can see the line executed inside the fun01() function is showed with a starting + which is the debugger in action.
@bash-o-logist Even if I add variable or if/then/else conditional constructs I still get all the debug info:
Executing again:
在 ksh 中,使用
typeset -ft function-name
跟踪函数In ksh use
typeset -ft function-name
to trace into a function我有类似的问题,最后我为 Bash 编写了自己的调试器。试试吧! ...我希望它会对您有所帮助 https://sourceforge.net/projects/bashdebugingbash/
I had similar question and I ended in writing my own debbuger for Bash. Try it! ... I hope it will help you https://sourceforge.net/projects/bashdebugingbash/
这就是我强制调试在 bash 脚本中的功能块内部打开或关闭的方法。
如果脚本启动时调试已打开,则它会在功能块内打开。每个功能块都有一个开始和结束消息,以便于跟踪。
这是为了运行时调试。最好将输出重定向到日志文件以便稍后分析,例如
在开始时打开调试的示例输出
在开始时关闭调试的示例输出
脚本
This is how I force debugging to turn on or off inside function blocks in bash scripts.
If debugging is on when the script starts, it is turned on inside function blocks. Each function block has a start and end message for easier tracing.
This is for runtime debugging. Best to redirect the output to a log file for analysis later, e.g.
Example output with debugging on at the start
Example output with debugging off at the start
THE SCRIPT