shell 语法检查和set -x对代码的跟踪

本文介绍了在编写脚本时如何进行语法检测和代码追踪,包括使用bash命令解释器进行语法检查,设置脚本追踪模式以追踪代码执行过程,以及通过set命令调整脚本执行的环境变量来获取更详细的执行信息。

from http://www.unixcom.cn/thread-3019-1-1.html

在写脚本时。开始会出一些错误。为了排除这些错误,所以我们需要对自己的脚本检测。


系统的检测方法:
检测语法的:(不能检测到逻辑错误。只能检测到语法错误)
[root@localhost shell]# bash -n 2.sh (以bash的命令解释器去检测所有语法)
或者在脚本里写
#!/bin/bash -n

set是对你要检测代码的打印
set -x 是开启 set +x是关闭 set -o是查看 (xtrace),set去追中一段代码的显示情况。
[root@localhost shell]# set -x
++ echo -ne '\033]0;root@localhost:/shell'
[root@localhost shell]# set -o
+ set -o
allexport off
braceexpand on
emacs on
errexit off
errtrace off
functrace off
hashall on
histexpand on
history on
ignoreeof off
interactive-comments on
keyword off
monitor on
noclobber off
noexec off
noglob off
nolog off
notify off
nounset off
onecmd off
physical off
pipefail off
posix off
privileged off
verbose off
vi off
xtrace on (开启)
++ echo -ne '\033]0;root@localhost:/shell'
[root@localhost shell]#
++ echo -ne '\033]0;root@localhost:/shell'
[root@localhost shell]# var=123
+ var=123
++ echo -ne '\033]0;root@localhost:/shell'
[root@localhost shell]# echo $var
+ echo 123
123
++ echo -ne '\033]0;root@localhost:/shell'
[root@localhost shell]# set -x
+ set -x
++ echo -ne '\033]0;root@localhost:/shell'
[root@localhost shell]# set +x
+ set +x
[root@localhost shell]# set -o
allexport off
braceexpand on
emacs on
errexit off
errtrace off
functrace off
hashall on
histexpand on
history on
ignoreeof off
interactive-comments on
keyword off
monitor on
noclobber off
noexec off
noglob off
nolog off
notify off
nounset off
onecmd off
physical off
pipefail off
posix off
privileged off
verbose off
vi off
xtrace off
在脚本里的可以这样写(不过这个是对所有脚本打印),可以在一断代码追踪
#!/bin/bash
set -x
.....
set -x
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值