#常见的调试命令工具
1.使用bash命令参数调试
#使用
[root@game ~]# sh [-xvn] test.sh #参数解释:
-x:将执行的脚本内容输出出来,可以看到执行的过程
-n:不执行脚本,检查脚本语法是否有问题,给出错误的提示
-v:执行脚本时,先将脚本的内容输出到屏幕上,再执行脚本,如果有错误给出错误提示
#示例
使用-n参数进行语法检查
#说明:不会执行脚本,只检查有无语法错误,如果没有检测到,就无输出
[root@game scripts]# cat test7.sh
#!/bin/bash
echo "guoke123"
[root@game scripts]# sh -n test7.sh
#脚本没有语法错误,所以没有输出 #演示脚本存在语法错误
#!/bin/bash
if [ `netstat -untpl | grep httpd | wc -l` -gt ];then
echo "httpd is Running
else
echo "httpd service down" | mail -s "httpd" @qq.com
systemctl restart httpd
fi
[root@game scripts]# sh -n test1.sh
test1.sh: line : unexpected EOF while looking for matching `"'
test1.sh: line : syntax error: unexpected end of file
#提示:第5行结尾没有双引号

-v参数:打印错误

[root@game scripts]# sh -v test1.sh
#!/bin/bash
if [ `netstat -untpl | grep httpd | wc -l` -gt ];then
echo "httpd is Running
else
echo "httpd service down" | mail -s "httpd" @qq.com
systemctl restart httpd
fi
test1.sh: line : unexpected EOF while looking for matching `"'
test1.sh: line : syntax error: unexpected end of file

-x参数:打印执行过程

#!/bin/bash
if [ `netstat -untpl | grep httpd | wc -l` -gt ];then
echo "httpd is Running"
else
echo "httpd service down" | mail -s "httpd" @qq.com
systemctl restart httpd
fi #打印执行过程
[root@game scripts]# sh -x test1.sh
++ netstat -untpl
++ wc -l
++ grep httpd
+ '[' -gt ']'
+ echo 'httpd service down'
+ mail -s httpd @qq.com
+ systemctl restart httpd

2.使用set命令调试

#常用选项
set -n :读命令但并不执行
set -v : 显示读取的所有行
set -x : 显示所有命令及其参数

#使用

使用set -x可以缩小调试的作用域范围
set -x开启调试功能,set +x关闭调试功能 #示例
#!/bin/bash
set -x
for i in `seq `
do
for n in `seq `
do
[ $i -ge $n ] && echo -en "$i x $n" = $(expr $i \* $n)
done
set +x
echo " "
done #执行效果
[root@game scripts]# sh test6.sh
++ seq
+ for i in '`seq 9`'
++ seq
+ for n in '`seq 9`'
+ '[' -ge ']'
++ expr '*'
+ echo -en '1 x 1' =
x = + for n in '`seq 9`'
+ '[' -ge ']'
.....
+ for n in '`seq 9`'
+ '[' -ge ']'
+ set +x
#提示:只调试了set -x 和set +x 这个作用域

3.echo命令调试

一般在可能出现问题的脚本的重要部分加入echo命令

#示例
[root@game scripts]# cat test8.sh
#!/bin/bash
read -p "please input tow num:" a b
echo $a $b
exit #执行效果
[root@game scripts]# sh test8.sh
please input tow num:

4.bashdb

shell调试器bashdb是一个类似GDB的调试工具,可以完成对shell脚本的断点设置、单步执行、变量观察等许多功能。

5.shellcheck

shellcheck是一个可检查sh/bash脚本和命令语法的小工具

#常见的shell脚本错误示例
#1.中括号两端没有空格
[root@game scripts]# cat test.sh
#!/bin/bash yum install net-tools -y >/dev/null
if [$? -eq ]
then
echo "install success"
else
echo "install fail"
fi #执行:报错
[root@game scripts]# sh test.sh
test.sh: line : [: command not found
install fail
#提示:错误在第四行

#2.成对的符号没有写全,漏写

#成对的符号例如:()、[]、""、''等
#示例[]中括号没有写全
[root@game scripts]# cat test1.sh
#!/bin/bash if [ `netstat -untpl | grep httpd | wc -l` -gt ;then
echo "httpd is Running"
else
echo "httpd service down" | mail -s "httpd" @qq.com
systemctl restart httpd
fi #执行效果
[root@game scripts]# sh test1.sh
test1.sh: line : [: missing `]'

#3.if条件语句缺少结尾关键字

[root@game scripts]# cat test2.sh
#!/bin/bash if [ `netstat -untpl | grep mysqld | wc -l` -gt ];then
echo "mysqld is Running"
else
echo "mysqld service down" | mail -s "mysqld" @qq.com
systemctl restart mysqld #执行效果
[root@game scripts]# sh test2.sh
test2.sh: line : syntax error: unexpected end of file
#执行脚本会提示第8行语法错误

#4.循环语句缺少关键字

#示例1:for循环的done少了个e
[root@game scripts]# cat test3.sh
#!/bin/bash
usage(){
echo "directory not found"
} [ ! -d /test ] && usage && exit
cd /test for i in `ls`
do
echo $i
don #执行效果
[root@game scripts]# sh test3.sh
test3.sh: line : syntax error: unexpected end of file #示例2:if条件语句的then少了n
[root@game scripts]# cat test2.sh
#!/bin/bash if [ `netstat -untpl | grep mysqld | wc -l` -gt ];the
echo "mysqld is Running"
else
echo "mysqld service down" | mail -s "mysqld" @qq.com
systemctl restart mysqld
fi
#执行效果
[root@game scripts]# sh test2.sh
test2.sh: line : syntax error near unexpected token `else'
test2.sh: line : `else'
#执行脚本之后会提示语法错误,需要注意的是shell脚本解释器一般不会很精确的定位到错误,需要上下联都看一下

#总结

在进行脚本编写的过程中,应该注重书写的规范性,成对符号或是循环语句应一次写完,再写相应的内容,避免不必要的麻烦,提升开发的效率

 

总结一篇shell调试技巧及常见的脚本错误的更多相关文章

  1. Visual Studio 调试技巧:10 篇热文汇总

    本文精选了 DotNet  2017年11月份的10篇热门文章.其中有技术分享.技术资源. 注:以下文章,点击标题即可阅读 <Visual Studio的调试技巧 > 调试技巧是衡量程序员 ...

  2. (转)Intellij IDEA 2017 debug断点调试技巧与总结详解篇

    背景:详细介绍idea的debug调试过程 Intellij IDEA 2017 debug断点调试技巧与总结详解篇

  3. shell脚本的调试技巧

    请参考文章:http://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/index.html 读后的感觉,还是用shell的选项灵活,方便. ...

  4. GDB调试技巧:总结篇

    目录 一 写在开头 1.1 本文内容 二 学习资料 三 常用命令 四 调试技巧 注:原创不易,转载请务必注明原作者和出处,感谢支持! 一 写在开头 1.1 本文内容 总结GDB调试的一些常用命令和调试 ...

  5. Linux c c++ 开发调试技巧

    看到一篇介绍 linux c/c++ 开发调试技巧的文章,感觉挺使用,哪来和大家分享. 通向 UNIX 天堂的 10 个阶梯Author: Arpan Sen, 高级技术人员, Systems Doc ...

  6. iOS开发之Xcode常用调试技巧总结

    转载自:iOS开发之Xcode常用调试技巧总结 最近在面试,面试过程中问到了一些Xcode常用的调试技巧问题.平常开发过程中用的还挺顺手的,但你要突然让我说,确实一脸懵逼.Debug的技巧很多,比如最 ...

  7. 《Debug Hacks》和调试技巧【转】

    转自:https://blog.csdn.net/sdulibh/article/details/46462529 Debug Hacks 作者为吉冈弘隆.大和一洋.大岩尚宏.安部东洋.吉田俊辅,有中 ...

  8. VS调试技巧,提高调试效率(转):

    如果你还没有使用过这些技巧,希望这篇博文能帮你发现它们. 它们学起来很容易,能帮你节省很多时间. 运行到光标(Ctrl+ F10) 我经常看见人们是这样来调试应用程序的: 他们在应用程序需要调试的代码 ...

  9. Visual Studio的调试技巧

    Visual Studio的调试技巧 [原文地址] Debugging Tips with Visual Studio 2010 [原文发表日期] 2010/8/19 10:48 AM 这是我写的关于 ...

随机推荐

  1. 【BZOJ4631】踩气球 题解(线段树)

    题目链接 ---------------------- 题目大意:给定一个长度为$n$的序列${a_i}$.现在有$m$个区间$[l_i,r_i]$和$q$个操作,每次选取一个$x$使得$a_x--$ ...

  2. 银弹谷零代码开发V百科|使用技巧:OMG!这些时间日期函数太好用了吧,盘它

    银弹谷零代码开发V百科|使用技巧:OMG!这些时间日期函数太好用了吧,盘它 Hello~everybody!小V又来咯!这次小V给大家带来的是零代码开发V平台常用的时间日期函数.小V知道我们平时常常会 ...

  3. Springboot中如何引入本地jar包,并通过maven把项目成功打包成jar包部署

    最近尝试引入阿里云的短信验证码,阿里云的core sdk是maven就有的,但是短信相关的jar包却不是放在maven的,所以得引入本地的下载回来的jar包.本地开发直接引入,idea是可以直接跑调用 ...

  4. Kaggle-SQL(1)

    Getting-started-with-sql-and-bigquery 教程 结构化查询语言(SQL)是数据库使用的编程语言,它是任何数据科学家的一项重要技能. 在本课程中,您将使用BigQuer ...

  5. 初学者都在坑里!不要在Python中使用“+”来连接字符串

    很多初学者都像我一样,最开始使用Python时,会不自觉地使用“+”来连接字符串,就像在许多其他编程语言(比如Java)中那样,因为这样既直观又容易. 但我很快意识到成熟的开发人员似乎更喜欢使用.jo ...

  6. Python下载网络图片方法汇总与实现

    本文介绍下载python下载网络图片的方法,包括通过图片url直接下载.通过re/beautifulSoup解析html下载以及对动态网页的处理等. ​ 很多人学习python,不知道从何学起.很多人 ...

  7. Django Web 测试

    Django 单元测试 模拟浏览器发起请求,测试 web 功能.只是简单记录一下怎么使用. 环境 Win10 Python2.7 Django 1.8.11 MySQL5.6 项目结构 大致如下 my ...

  8. Catalina 默认使用zsh了,你可习惯

    zsh 成为默认 shell 淘汰掉我的旧MBP换新后,欢天喜地打开Terminal,感觉有点不对,提示符什么时候变成了 %. 查询了一些资料发现,原来在2019年WWDC期间,苹果推出了macOS ...

  9. tableau用户分类

    1.观察消费金额的分布 直接[消费金额]直方图趋势不明显的时候,可以考虑将金额对数化处理 这样看起来就近似个正态分布了 2.怎么看超市卖的最好的产品 更深层次的分析怎么做呢? 这个聚合字段在数据源不会 ...

  10. C#LeetCode刷题之#21-合并两个有序链表(Merge Two Sorted Lists)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3818 访问. 将两个有序链表合并为一个新的有序链表并返回.新链表 ...