Linux的shell编程(六)-shell的简单调试
-
说明: 任何语编程都有对应的调试工具??如java有Debug、mysql有调试工具、js有调试工具等。shell也不例外shell的语法检测:相当于java的编译
-
shell语法检测:
sh -n ./test.sh (sh是/bin/sh 是系统提供的可执行脚本)
- shell的普通调试:
sh -x ./test.sh
-
例如调试test.sh的内容
- 创建的test.hs
#!/bin/bash echo "welcome to shell debug" for i in 1 2 3 4 5 6 do echo $i done echo "shell debug is over"
- 执行sh -x ./test.sh该语句后如下:
+ echo 'welcome to shell debug' welcome to shell debug + for i in 1 2 3 4 5 6 + echo 1 1 + for i in 1 2 3 4 5 6 + echo 2 2 + for i in 1 2 3 4 5 6 + echo 3 3 + for i in 1 2 3 4 5 6 + echo 4 4 + for i in 1 2 3 4 5 6 + echo 5 5 + for i in 1 2 3 4 5 6 + echo 6 6 + echo 'shell debug is over' shell debug is over ``` * 说明进入调试模式后,Shell依次执行读入的语句,产生的输出中有的带加号,有的不带,带加号表示该条语句是Shell执行的。不带加号表示该语句是Shell产生的输出。
-
shell的中断调试:(在shell中添加一个睡眠,保证可以有时间中断调试 sleep 3 睡眠3秒执行下一个语句)
- 创建源文件
#!/bin/bash echo "welcome to shell debug" for i in 1 2 3 4 5 6 do echo $i sleep 3 done echo "shell debug is over"
- 在调试过程中可以按Ctrl + Z中断调试,观察结果,然后再按fg键继续调试即可。(先按f在按g键)
-
使用调试工具-bashdb
- 【功能】: 类似于GDB的调试工具,可以完成对shell脚本的断点设置,单步执行,变量观察等许多功能
- 【场合】: 脚本比较大时,通过-x参数调试时已不方便时.
- 【用法】:
bashdb -c script.sh bashdb script.sh bashdb --debugger script.sh
-
【说明】:该工具默认未安装,当前最新版本为:4.4-0.92,下载目录:http://bashdb.sourceforge.NET/
- 1). 如果是ubuntu系统,直接用apt-get来安装
apt-get install bashdb
- 2). 如果是Centos等版本,使用windows下载后,编译,安装,大致步骤如下:
下载:https://sourceforge.net/projects/bashdb/files/bashdb/ [root@hadoop007 ~]# tar -xzvf bashdb-4.4-0.92.tar.gz -C /usr/src [root@hadoop007 ~]# cd /usr/src/bashdb-4.4-0.92 [root@hadoop007 ~]# ./configure [root@hadoop007 ~]# make install
-
【示例】
vi /home/test1.sh #!/bin/bash echo "----------------begin-----------------" MAX=3 for ((i = 0; i < MAX; i++)) do nowdate=`date -d"-$i day" +%Y-%m-%d` echo $nowdate done echo "----------------end-----------------"
- 调试命令:
./bashdb --debugger /home/test.sh [root@hadoop007 bashdb-4.4-0.92]# ./bashdb --debugger /home/test.sh bash debugger, bashdb, release 4.4-0.92 Copyright 2002, 2003, 2004, 2006-2012, 2014 Rocky Bernstein This is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. (/home/test.sh:3): 3: echo "----------------begin-----------------" bashdb<0> n #执行下一条语句 ----------------begin----------------- (/home/test.sh:4): 4: MAX=3 bashdb<1> l #列出当前行上下各5行,总共10行 1: #!/bin/bash 2: 3: echo "----------------begin-----------------" 4: => MAX=3 5: for ((i = 0; i < MAX; i++)) 6: do 7: nowdate=`date -d"-$i day" +%Y-%m-%d` 8: echo $nowdate 9: done 10: echo "----------------end-----------------" bashdb<2> b 7 #在行号为7的行设置断点 Breakpoint 1 set in file /home/test.sh, line 7. bashdb<3> c #继续运行 Breakpoint 1 hit (1 times). (/home/test.sh:7): 7: nowdate=`date -d"-$i day" +%Y-%m-%d` bashdb<4> print $i 0 bashdb<5> n (/home/test.sh:8): 8: echo $nowdate bashdb<6> print $nowdate 2017-03-20 bashdb<7> c 10 #单步往下运行10步 One-time breakpoint 2 set in file /home/test.sh, line 10. 2017-03-20 Breakpoint 1 hit (2 times). (/home/test.sh:7): 7: nowdate=`date -d"-$i day" +%Y-%m-%d` bashdb<8> finish #运行到结束 Breakpoint 1 hit (3 times). (/home/test.sh:7): 7: nowdate=`date -d"-$i day" +%Y-%m-%d` date -d"-$i day" +%Y-%m-%d bashdb<(9)> finish 2017-03-19 Breakpoint 1 hit (4 times). (/home/test.sh:7): 7: nowdate=`date -d"-$i day" +%Y-%m-%d` bashdb<10> finish Breakpoint 1 hit (5 times). (/home/test.sh:7): 7: nowdate=`date -d"-$i day" +%Y-%m-%d` date -d"-$i day" +%Y-%m-%d bashdb<(11)> finish 2017-03-18 (/home/test.sh:10): 10: echo "----------------end-----------------" bashdb<12> q #退出 bashdb: That's all, folks...
- 【常用命令】
l 列出当前行上下各5行,总共10行 q|quit 退出 h 帮助 /for/ 向后搜索字符串for ?for? 向前搜索字符串for x 1+2 计算算术表达式的值 !! ls -laRt 执行shell命令 n 执行下一条语句 s 4 单步执行4次,如遇到函数则进入函数里面 b 4 在行号4处设置断点 del 4 删除行号为4的断点 c 10 一直执行到行号10处 R|run 重新执行当前调试脚本 finish 执行到程序最后