bash:调试选项和功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7415235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash: debug option and functions
提问by Florin Ghita
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.
但是如果我在 中有一个函数myscript.sh,则该函数中的代码不受 -x 选项的影响。它只写入输出函数的名称。
How to obtain debugging output for functions in bash scripts?
如何获取 bash 脚本中函数的调试输出?
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)
按照 ztank1013 的回应,我才意识到我使用的是 ksh,而不是 bash。似乎 bash 在我的系统中默认启用了 functrace 选项(感谢 bash-o-logist)
I am satisfied, but for the community I maintain the question open for ksh.
我很满意,但对于社区,我保持对 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.shis:
的输出ksh -x ./testdebug.sh是:
+ a=2
+ [ 2 -eq 2 ]
+ echo mimi
mimi
+ testering
lili
+ exit
So, for ksh, what's the trick?
那么,对于 ksh,有什么技巧呢?
(If no answer will come, the 'correct' will go to bash-o-logist.)
(如果没有答案,“正确”将交给 bash-o-logist。)
回答by bash-o-logist
With bash, you can use functraceoption in your script
使用 bash,您可以functrace在脚本中使用选项
set -o functrace
See manpage for bashfor other debugger options.
有关bash其他调试器选项,请参见联机帮助页。
回答by ztank1013
I cannot reproduce your problem, in fact given my test script (debug.sh):
事实上,鉴于我的测试脚本 (debug.sh),我无法重现您的问题:
[root ~]# cat debug.sh
#!/bin/bash
fun01 () {
echo "BUT HERE I am inside the function fun01() body"
}
echo "HERE I am outside the function fun01() body!"
sleep 2
fun01
exit
I run it with debug option turned off:
我在关闭调试选项的情况下运行它:
[root ~]# ./debug.sh
HERE I am outside the function fun01() body!
BUT HERE I am inside the function fun01() body
and turned on (bash -x ...):
并打开(bash -x ...):
[root ~]# bash -x ./debug.sh
+ echo 'HERE I am outside the function fun01() body!'
HERE I am outside the function fun01() body!
+ sleep 2
+ fun01
+ echo 'BUT HERE I am inside the function fun01() body'
BUT HERE I am inside the function fun01() body
+ exit
As far as I can see the line executed inside the fun01() function is showed with a starting + which is the debugger in action.
就我所见,在 fun01() 函数中执行的行显示有一个起始 +,这是正在运行的调试器。
@bash-o-logist Even if I add variable or if/then/else conditional constructs I still get all the debug info:
@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
Executing again:
再次执行:
[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
回答by cdarke
In ksh use typeset -ft function-nameto trace into a function
在 ksh 中使用typeset -ft function-name跟踪到一个函数
回答by abadjm
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 编写了自己的调试器。尝试一下!...我希望它能帮助你https://sourceforge.net/projects/bashdebugingbash/
回答by keithchristian
This is how I force debugging to turn on or off inside function blocks in bash scripts.
这就是我如何强制调试在 bash 脚本中的功能块内打开或关闭。
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.
这是用于运行时调试。最好将输出重定向到日志文件以供稍后分析,例如
bash -x ./runtime_bash_debugging>log 2>&1
-- or --
./runtime_bash_debugging>log 2>&1
Example output with debugging on at the start
在开始时调试的示例输出
$ 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
Example output with debugging off at the start
开始时关闭调试的示例输出
$ ./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
THE SCRIPT
剧本
#!/bin/bash
# runtime bash debugging
fn_check_xtrace() {
XTRACE_BEGIN_STATE=`set -o|awk '=="xtrace"{print }'`
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

