sh 命令调试选项(推荐)
使用方法:
字符串读取脚本。
$ sh -c 'if [ 1 -lt 2 ];then echo "true"; else echo "false"; fi'true
注:临时测试 shell 语法或者小段脚本时使用。
检查脚本是否存在语法错误。
$ sh -n daodaotest.sh
跟踪调试 shell 脚本,将执行的每一条命令结果依次打印出来。
$ sh -x daodaotest.sh+ '[' 2 -lt 2 ']'+ COUNT=3+ PARAMETER=daodaotest+ (( i = 1 ))+ (( i <= 3 ))+ echo '第 1 遍打印:daodaotest'第 1 遍打印:daodaotest+ (( i++ ))+ (( i <= 3 ))+ echo '第 2 遍打印:daodaotest'第 2 遍打印:daodaotest+ (( i++ ))+ (( i <= 3 ))+ echo '第 3 遍打印:daodaotest'第 3 遍打印:daodaotest+ (( i++ ))+ (( i <= 3 ))+ exit 0
跟踪调试 shell 脚本,将执行的每一条命令和结果依次打印出来。
$ sh -xv daodaotest.sh#!/bin/bash# 调试脚本示例# 使用方法usage() { echo "Usage: sh $0 COUNT PARAMETER" echo "t COUNT 循环打印次数" echo "t PARAMETER 打印字符串" echo "示例:" echo "t 1. 打印 daodaotest 2 次" echo "t sh $0 2 daodaotest"}# 判断参数if [ $# -lt 2 ];then usage exit 1fi+ '[' 2 -lt 2 ']'# 打印次数COUNT=$1+ COUNT=3# 打印字符串PARAMETER=$2+ PARAMETER=daodaotest# 循环打印for (( i = 1; i <= $COUNT; i++));doecho "第 $i 遍打印:$PARAMETER"done+ (( i = 1 ))+ (( i <= 3 ))+ echo '第 1 遍打印:daodaotest'第 1 遍打印:daodaotest+ (( i++ ))+ (( i <= 3 ))+ echo '第 2 遍打印:daodaotest'第 2 遍打印:daodaotest+ (( i++ ))+ (( i <= 3 ))+ echo '第 3 遍打印:daodaotest'第 3 遍打印:daodaotest+ (( i++ ))+ (( i <= 3 ))exit 0+ exit 0
注:本人最常用-x参数,能解决 90% 的脚本调试问题。
set 方法
在脚本中用set命令。
使用方法:
$ cat daodaotest.shset -xv..... 省略set +xv$ sh daodaotest.sh 2 daodaotest# 使用方法usage() { echo "Usage: sh $0 COUNT PARAMETER" echo "t COUNT 循环打印次数" echo "t PARAMETER 打印字符串" echo "示例:" echo "t 1. 打印 daodaotest 2 次" echo "t sh $0 2 daodaotest"}# 判断参数if [ $# -lt 2 ];then usage exit 1fi+ '[' 2 -lt 2 ']'# 打印次数COUNT=$1+ COUNT=2# 打印字符串PARAMETER=$2+ PARAMETER=daodaotest# 循环打印for (( i = 1; i <= $COUNT; i++));doecho "第 $i 遍打印:$PARAMETER"done+ (( i = 1 ))+ (( i <= 2 ))+ echo '第 1 遍打印:daodaotest'第 1 遍打印:daodaotest+ (( i++ ))+ (( i <= 2 ))+ echo '第 2 遍打印:daodaotest'第 2 遍打印:daodaotest+ (( i++ ))+ (( i <= 2 ))exit 0+ exit 0
注: 在脚本非常复杂时,set可以进行局部调试,在需要调试的代码块前后设置即可。
工具
shellcheck
shell 脚本静态检查工具,可以帮助你写出刚好的脚本。
官 :
https://www.shellcheck.net/手册:
https://github.com/koalaman/shellcheck
使用方法:
$ shellcheck daodaotest.shIn daodaotest.sh line 8: echo "t COUNT 循环打印次数" ^---------------^ SC2028: echo may not expand escape sequences. Use printf.In daodaotest.sh line 9: echo "t PARAMETER 打印字符串" ^------------------^ SC2028: echo may not expand escape sequences. Use printf.In daodaotest.sh line 11: echo "t 1. 打印 daodaotest 2 次" ^-----------------------^ SC2028: echo may not expand escape sequences. Use printf.In daodaotest.sh line 12: echo "t sh $0 2 daodaotest" ^---------------------^ SC2028: echo may not expand escape sequences. Use printf.In daodaotest.sh line 28:for (( i = 1; i <= $COUNT; i++)); ^----^ SC2004: $/${} is unnecessary on arithmetic variables.For more information: https://www.shellcheck.net/wiki/SC2028 -- echo may not expand escape sequen... https://www.shellcheck.net/wiki/SC2004 -- $/${} is unnecessary on arithmeti...
BASH Debugger
bashdb 是一个类 GDB 的调试工具,可以运行断点设置、变量查看等常见调试操作。
官 :
http://bashdb.sourceforge.net/
常用参数:
h:查看帮助。help 命令:命令的具体信息。n:执行下一条语句。s n:单步执行 n 次。b n:在行 n 处设置断点。c n:一直执行到行 n 处。l:显示上下文代码。print:打印变量值,例如 print $COUNT。finish:执行到程序最后或断点处。q or exit:退出。R:重新执行。
使用方法:
$ bashdb --debug daodaotest.sh bash debugger, bashdb, release 5.0-1.1.2Copyright 2002-2004, 2006-2012, 2014, 2016-2019 Rocky BernsteinThis is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.(/Users/jlh/tmp/daodaotest.sh:15):15: if [ $# -lt 2 ];# 执行下一条语句。bashdb<0> n(/Users/jlh/tmp/daodaotest.sh:22):22: COUNT=$1# 显示上下文代码。bashdb<1> l 17: usage 18: exit 1 19: fi 20: 21: # 打印次数 22: => COUNT=$1 23: # 打印字符串 24: PARAMETER=$2 25: 26: # 循环打印# 单步执行 3 次。bashdb<2> s 3(/Users/jlh/tmp/daodaotest.sh:27):27: for (( i = 1; i <= $COUNT; i++));((i <= 3))bashdb<3> l 22: COUNT=$1 23: # 打印字符串 24: PARAMETER=$2 25: 26: # 循环打印 27: => for (( i = 1; i <= $COUNT; i++)); 28: do 29: echo "第 $i 遍打印:$PARAMETER" 30: done 31:# 打印 $COUNT 参数值bashdb<4> print $COUNT3# 执行到最后bashdb<5> finish第 1 遍打印:daodaotest第 2 遍打印:daodaotest第 3 遍打印:daodaotestDebugged program terminated normally. Use q to quit or R to restart.# 退出bashdb<6> qbashdb: That's all, folks...
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!