Shell调试技术小结
Shell调试技术总结4 a65 b26 c307 while :8 do9 if (( $a 10 ))10 ? then break11 fi12 ? let a$a
Shell调试技术总结
4 a=6
5 b=26 c=307 while :8 do9 if (( $a >= 10 ))10 ? then break11 fi12 ? let "a=$a+2"13 ? let "b=$b*2"14 ? ? let "c=$c-10"15 done[root@localhost shell]# sh trapdebugbefore execute line: 4 a=,b=,c=before execute line: 5 a=6,b=,c=before execute line: 6 a=6,b=2,c=before execute line: 7 a=6,b=2,c=30before execute line: 9 a=6,b=2,c=30before execute line: 12 a=6,b=2,c=30before execute line: 13 a=8,b=2,c=30before execute line: 14 a=8,b=4,c=30before execute line: 7 a=8,b=4,c=20before execute line: 9 a=8,b=4,c=20before execute line: 12 a=8,b=4,c=20before execute line: 13 a=10,b=4,c=20before execute line: 14 a=10,b=8,c=20before execute line: 7 a=10,b=8,c=10before execute line: 9 a=10,b=8,c=10before execute line: 10 a=10,b=8,c=10before execute line: 16 a=10,b=8,c=10?
2 then
3 echo "Debugging information: "4 、、、、5 fi?
5 then
6 $@7 fi8 }9 a=010 b=211 c=10012 DEBUG echo "a=$a b=$b c =$c" #第1个调试钩子13 while :14 do15 DEBUG echo "a=$a b=$b c =$c" #第个调试钩子16 if (( $a >= 10 ))17 then18 break19 fi20 let "a=$a+2"21 let "b=$b*2"22 let "c=$c-10"23 done[root@localhost shell]# export DEBUG=true[root@localhost shell]# sh debugblocka=0 b=2 c =100a=0 b=2 c =100a=2 b=4 c =90a=4 b=8 c =80a=6 b=16 c =70a=8 b=32 c =60a=10 b=64 c =50?
6 echo "ok ,continue";
7 elif [ "$yn" == "$N" ] || [ "$yn" == "$n"]; then8 echo "oh ,interrupt!";9 else echo "I don't know what is your choise"10 fi[root@localhost shell]# sh else+ export PATH+ read -p 'Please input (Y/N)'Please input (Y/N)y+ '[' '' == '' ']'+ echo 'ok ,continue'ok ,continue?
5 then
6 return 17 else8 return 09 fi10 }11 echroot()12 {13 isroot14 if [ "$?" -ne 0 ]15 then16 echo "I'm not root"17 else18 echo "I'm root"19 fi20 }而且21 #对PS4赋值,定制-x选项的提示符22 export PS4='+{$LINENO: ${FUNCNAME[0]}:${FUNCNAME[1]}}'23 echroot[root@localhost shell]# sh -x nestfun+ export 'PS4=+{$LINENO: ${FUNCNAME[0]}:${FUNCNAME[1]}}'+ PS4='+{$LINENO: ${FUNCNAME[0]}:${FUNCNAME[1]}}'+{23: :}echroot+{13: echroot:main}isroot+{4: isroot:echroot}'[' 0 -ne 0 ']'+{8: isroot:echroot}return 0+{14: echroot:main}'[' 0 -ne 0 ']'+{18: echroot:main}echo 'I'\''m root'I'm root?
总结:针对Shell脚本中所遇到的错误进行了分析,对于逻辑错误进行的全面分析,trap能够代替echo方便的用于脚本输出信息,对撼树进行跟踪等。tee运用管道,便于清晰的看出管道间的数据流向,调试钩子借鉴于高级程序语言,是很好的编程风格,Shell选项不改变脚本内容,-n和-x选项是常见的脚本调试手段,-n主要用于脚本语法错误的调试,-x主要用于脚本的逻辑错误的调试。