Common shell script debugging method

When I had just started to learn shell scripts, in addition to know some of the information output by echo, but did not know other methods rely solely on echo for errors, debugging more difficult and the process cumbersome and inefficient. I used some shell script debugging method in this paper, we hope to help beginners the shell.

sh command debug option (recommended)

Options Explanation
-c From -creading the command string.
-n Check for syntax errors, but does not actually perform.
-x The results of each command and executed sequentially printed.
-v Executed script command prints to standard output.

Instructions:

String read the script.

$ sh -c 'if [ 1 -lt 2 ];then echo "true"; else echo "false"; fi'
true

NOTE: When using temporary test small pieces of shell syntax or script.

Check the script for syntax errors.

$ sh -n daodaotest.sh

Trace debug shell script, the result of each command to be executed sequentially printed.

$ 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

Trace debugging shell scripts, each command will be performed and results sequentially printed.

$ 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 1
fi
+ '[' 2 -lt 2 ']'

# 打印次数
COUNT=$1
+ COUNT=3
# 打印字符串
PARAMETER=$2
+ PARAMETER=daodaotest

# 循环打印
for (( i = 1; i <= $COUNT; i++));
do
echo "第 $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

Note: it is the most commonly used -xparameters, can solve 90% of the script debugging problems.

set method

In the script setcommand.

  • set -xvEnable representation;
  • set +xvDisable.

Instructions:

$ cat daodaotest.sh
set -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 1
fi
+ '[' 2 -lt 2 ']'

# 打印次数
COUNT=$1
+ COUNT=2
# 打印字符串
PARAMETER=$2
+ PARAMETER=daodaotest

# 循环打印
for (( i = 1; i <= $COUNT; i++));
do
echo "第 $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

NOTE: When a script is very complex, setcan debug locally provided before and after the block of code needs to debug.

tool

shellcheck

shell script static checking tool that helps you just write the script.

Official website: https://www.shellcheck.net/
Manual: https://github.com/koalaman/shellcheck

Instructions:

$ shellcheck daodaotest.sh

In 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 is a class GDB debugging tool, you can run to set breakpoints, variable view and other common debugging operations.

Official website: http://bashdb.sourceforge.net/

Common parameters:

h:查看帮助。
help 命令:命令的具体信息。

n:执行下一条语句。
s n:单步执行 n 次。
b n:在行号 n 处设置断点。
c n:一直执行到行号 n 处。
l:显示上下文代码。

print:打印变量值,例如 print $COUNT。
finish:执行到程序最后或断点处。

q or exit:退出。

R:重新执行。

Instructions:

$ bashdb --debug daodaotest.sh
 bash debugger, bashdb, release 5.0-1.1.2

Copyright 2002-2004, 2006-2012, 2014, 2016-2019 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.

(/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 $COUNT
3

# 执行到最后
bashdb<5> finish
第 1 遍打印:daodaotest
第 2 遍打印:daodaotest
第 3 遍打印:daodaotest
Debugged program terminated normally. Use q to quit or R to restart.

# 退出
bashdb<6> q
bashdb: That's all, folks...

Guess you like

Origin www.cnblogs.com/daodaotest/p/12543054.html