Shell debugging and shell debugging
I. Introduction
This article comprehensively and systematically introduces the shell script debugging technology, including using echo, tee, trap and other commands to output key information, tracking variable values, and inserting debugging hooks into the script, use the "-n" option to check the syntax of shell scripts, and use the "-x" option to track shell scripts one by one, cleverly use the built-in shell variables to enhance the output information of the "-x" option.
Ii. shell debugging options
1) read only shell scripts, not actually executed, used to detect whether shell scripts have syntax errors
-n
2) Enable the shell interpreter to read and execute shell commands from a string for temporary testing of short scripts.
-c "string"
3) enter the tracking method, so that the shell will display every command line actually executed during script execution, and display a "+" number at the beginning of the line
-x
Example:
$ sh –x exp2.sh+ trap 'echo "before execute line:$LINENO, a=$a,b=$b,c=$c"' DEBUG++ echo 'before execute line:3, a=,b=,c='before execute line:3, a=,b=,c=+ a=1++ echo 'before execute line:4, a=1,b=,c='before execute line:4, a=1,b=,c=+ '[' 1 -eq 1 ']'++ echo 'before execute line:6, a=1,b=,c='before execute line:6, a=1,b=,c=+ b=2++ echo 'before execute line:10, a=1,b=2,c='before execute line:10, a=1,b=2,c=+ c=3++ echo 'before execute line:11, a=1,b=2,c=3'before execute line:11, a=1,b=2,c=3+ echo endend
Iii. shell debugging tool: bashdb
http://bashdb.sourceforge.net/
Reference: http://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/