您的位置:首页 > 运维架构 > Shell

Shell 调试

2015-09-21 21:25 537 查看
一、简介

本文全面系统地介绍了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/

安装

yum install -y bashdb

实例

http://zhangge.net/1517.html


参考:http://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: