Shell 调试


一、简介
 
本文全面系统地介绍了shell脚本调试技术,包括使用echo, tee, trap等命令输出关键信息,跟踪变量的值,在脚本中植入调试钩子,使用“-n”选项进行shell脚本的语法检查, 使用“-x”选项实现shell脚本逐条语句的跟踪,巧妙地利用shell的内置变量增强“-x”选项的输出信息等。
 
二、shell调试选项
 
1)只读取shell脚本,不实际执行,用于检测shell脚本是否存在语法错误
 
-n
 
2)使shell解释器从一个字符串中读取并执行shell指令,用于临时测试小段脚本
 
-c "string"
 
3)进入跟踪方式,使shell在执行脚本的过程中把它实际执行的每一个命令行显示出来,并且在行首显示一个"+"号
 
-x
示例:
$ sh –x exp2.sh
+ trap 'echo "before execute line:$LINENO, a=$a,b=$b,c=$c"' DEBUG
++ echo 'before execute line:3, a=,b=,c='
before execute line:3, a=,b=,c=
+ a=1
++ echo 'before execute line:4, a=1,b=,c='
before execute line:4, a=1,b=,c=
+ '[' 1 -eq 1 ']'
++ echo 'before execute line:6, a=1,b=,c='
before execute line:6, a=1,b=,c=
+ b=2
++ echo 'before execute line:10, a=1,b=2,c='
before execute line:10, a=1,b=2,c=
+ c=3
++ echo 'before execute line:11, a=1,b=2,c=3'
before execute line:11, a=1,b=2,c=3
+ echo end
end
 
三、shell调试工具:bashdb
 
http://bashdb.sourceforge.net/

相关内容