Shell脚本调试方法


1. sh –n test.sh :测试脚本的语法错误

sh –x test.sh /set –x  :跟踪所有命令行

sh –e tesh.sh /set –e  :如果一个命令失败就立即退出

2.debug=1

test $debug –gt 0 && echo “this is a debug info.”

3. trap 'command' signal

EXIT
 从一个函数中退出或整个脚本执行完毕
 
ERR
 当一条命令返回非零状态时(代表命令执行不成功)
 
DEBUG
 脚本中每一条命令执行之前 

trap 'echo “before execute :$LINENO, a=$a,b=$b”' DEBUG

相关内容