免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1732 | 回复: 0
打印 上一主题 下一主题

shell 语法检查和set -x对代码的跟踪 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-08 10:44 |只看该作者 |倒序浏览
在写脚本时。开始会出一些错误。为了排除这些错误,所以我们需要对自己的脚本检测。

系统的检测方法:
检测语法的:(不能检测到逻辑错误。只能检测到语法错误)
[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

-----------------------------------------------------------
文章来源:http://www.unixcom.cn/thread-3019-1-1.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP