shell script 的调试 debug

shell script的 debug有以下几种方式:

一、直接运行,如果有语法错误,系统会提示。

root@Test:~/bin$ sh morechoise.sh 
morechoise.sh: 7: morechoise.sh: choise[1]=learn english: not found
morechoise.sh: 8: morechoise.sh: choise[2]=yoga practice: not found
morechoise.sh: 9: morechoise.sh: choise[3]=read book: not found
morechoise.sh: 10: morechoise.sh: choise[4]=draw picture: not found
morechoise.sh: 11: morechoise.sh: choise[5]=learn techniques: not found
morechoise.sh: 12: morechoise.sh: choise[6]=Watch TV: not found
morechoise.sh: 13: morechoise.sh: choise[7]=cook: not found
morechoise.sh: 14: morechoise.sh: choise[8]=listen radio: not found
morechoise.sh: 18: morechoise.sh: while[: not found
morechoise.sh: 19: morechoise.sh: Syntax error: "do" unexpected
二、在运行前,可以使用 bash 的相关参数來进行判断

[root@Test ~]$ sh [-nvx] scripts.sh
选项与参数:
-n  :不用执行 script,仅检查语法的问题;
-v  :在执行 sccript 前,先将 scripts 的内容输出到屏幕上;
-x  :将使用到的 script 内容显示到屏幕上

示例:将 show_animal.sh 的执行过程全部列出来
[root@Test ~]$ sh -x show_animal.sh 
+ PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/bin
+ export PATH
+ for animal in dog cat elephant
+ echo 'There are dogs.... '
There are dogs....
+ for animal in dog cat elephant
+ echo 'There are cats.... '
There are cats....
+ for animal in dog cat elephant
+ echo 'There are elephants.... '
There are elephants....
   在加号后面的资料其实都是命令串, 以sh -x 方式将命令执行过程也显示出来, 如此可以判断程序代码执行到某段时会出现相关信息,通过显示完整的命令,也就能根据输出的错误信息来调试脚本。


猜你喜欢

转载自blog.csdn.net/heart_1014/article/details/54375940