Shell - 测试

Shell - 测试
任何语编程都有对应的调试工具如java有Debug、mysql有调试工具、js有调试工具等shell也不例外shell的语法检测相当于java的编译1. shell语法检测sh -n ./test.sh (sh是/bin/sh 是系统提供的可执行脚本)2. shell的普通调试sh -x ./test.sh如test.sh的内容如下#!/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 debugfor i in 1 2 3 4 5 6echo 11for i in 1 2 3 4 5 6echo 22for i in 1 2 3 4 5 6echo 33for i in 1 2 3 4 5 6echo 44for i in 1 2 3 4 5 6echo 55for i in 1 2 3 4 5 6echo 66echo ‘shell debug is over’shell debug is over3. 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键)4. 使用调试工具-bashdb【功能】: 类似于GDB的调试工具可以完成对shell脚本的断点设置单步执行变量观察等许多功能【场合】: 脚本比较大时通过-x参数调试时已不方便时.【用法】:bashdb -c script.shbashdb script.shbashdb --debugger script.sh【说明】:该工具默认未安装当前最新版本为:4.4-0.92,下载目录:http://bashdb.sourceforge.NET/1). 如果是ubuntu系统,直接用apt-get来安装apt-get install bashdb2). 如果是Centos等版本使用windows下载后编译安装大致步骤如下:下载https://sourceforge.net/projects/bashdb/files/bashdb/[roothadoop007 ~]# tar -xzvf bashdb-4.4-0.92.tar.gz -C /usr/src[roothadoop007 ~]# cd /usr/src/bashdb-4.4-0.92[roothadoop007 ~]# ./configure[roothadoop007 ~]# make install【示例】:vi /home/test1.sh#!/bin/bash echo ----------------begin----------------- MAX3 for ((i 0; i MAX; i)) do nowdatedate -d-$i day %Y-%m-%d echo $nowdate done echo ----------------end-----------------调试命令./bashdb --debugger /home/test.sh[roothadoop007 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----------------- bashdb0 n #执行下一条语句 ----------------begin----------------- (/home/test.sh:4): 4: MAX3 bashdb1 l #列出当前行上下各5行总共10行 1: #!/bin/bash 2: 3: echo ----------------begin----------------- 4: MAX3 5: for ((i 0; i MAX; i)) 6: do 7: nowdatedate -d-$i day %Y-%m-%d 8: echo $nowdate 9: done 10: echo ----------------end----------------- bashdb2 b 7 #在行号为7的行设置断点 Breakpoint 1 set in file /home/test.sh, line 7. bashdb3 c #继续运行 Breakpoint 1 hit (1 times). (/home/test.sh:7): 7: nowdatedate -d-$i day %Y-%m-%d bashdb4 print $i 0 bashdb5 n (/home/test.sh:8): 8: echo $nowdate bashdb6 print $nowdate 2017-03-20 bashdb7 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: nowdatedate -d-$i day %Y-%m-%d bashdb8 finish #运行到结束 Breakpoint 1 hit (3 times). (/home/test.sh:7): 7: nowdatedate -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: nowdatedate -d-$i day %Y-%m-%d bashdb10 finish Breakpoint 1 hit (5 times). (/home/test.sh:7): 7: nowdatedate -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----------------- bashdb12 q #退出 bashdb: Thats all, folks...