v>

shell脚本调试


shell script的追踪调试debug
在执行之前,可以直接用bash的相关参数来进行语法检查判断:
            [peter@study ~]$ sh [-nvx] scripts.sh 
            
选项与参数:
-n :不要执行script,仅查询语法的问题;
-v :再执行sccript前,先将scripts的内容输出到屏幕上;
-x :将使用到的script内容显示到屏幕上,这是很有用的参数!
测试dir_perm.sh有无语法的问题:
            [peter@study ~]$ sh -n dir_perm.sh #若语法没有问题,则不会显示任何信息! 
            
将show_animal.sh的执行过程列出来:
            [peter@study ~]$ 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的方式将指令执行过程也显示出来, 用户可以判断程序代码执行到哪一段时会出现相关的信息! 透过显示完整的指令串,就能够依据输出的错误信息来订正脚本了!
熟悉sh的用法,可以使你在管理Linux的过程中得心应手!
多看、多模仿、并加以修改成自己的样式是学习Shell scripts最快的手段了!
另外,Linux系统本来就有很多的服务启动脚本,如果要知道每个script所代表的功能是什么?
可以直接以vim进入该script去查阅一下,通常立刻就知道该script的目的了。
例如/etc/init.d/netconsole,用vim去查阅最前面的几行:
            # netconsole This loads the netconsole module with the configured parameters. 
            # chkconfig: - 50 50 # description: Initializes network console logging 
            # config: /etc/sysconfig/netconsole
            
意思是说这个脚本在设定网络终端机来应付登入,且配置文件在/etc/sysconfig/netconsole内!
如果你写的脚本也能够很清楚的交待,那就太棒了!

initroot编辑整理,转载请注明www.initroot.com

100次点赞 100次阅读