Linux Shell Program Debugging
The shell program is debugged by adding relevant debugging options when running the program or by adding related statements to the script, allowing the shell program to display some "debug information" for reference during execution. Of course, the user can also add some echo commands to the shell program for debugging and tracing in the appropriate location.
Method One: Add debugging options when you run the script:
"Usage" bash– Options ./shellscript.sh
A few common debugging options are as follows:
-e: If a command returns a non-0 exit status value (failed), exit.
-N: You do not need to execute the script just to check the syntax structure and return all syntax error messages.
-U: The non-set variable is treated as an error when displacing.
- v : Displays the lines when they are read into the shell.
- x : Commands and their arguments are displayed when the command is executed.
method Two: In the script program through SET Command Debug Program:
"Usage" set– Options , Set + Options #set common options for commands ditto
To be referenced inside a shell program as a set-select item, and set + Select to prevent the selection from functioning. If you only want to use certain selections on a part of a program, you can enclose that part with the two statements above.
(1) Non-variable exit (-u) and immediate exit (-e)
An unassigned variable exit attribute allows the user to check all variables and terminate the execution of the shell program if a variable that is referenced is not assigned. The shell typically allows the use of an invariant variable, in which case the value of the variable is empty. If an invariant variable is set to exit the selection, an error message is displayed once the invariant variable is used and the program is terminated. The left variable exits the Select-U.
When the shell runs, if it encounters a nonexistent or unenforceable command, a redirection failure, or an abnormal command end, the error message is displayed on the terminal screen without redirection, and the shell program continues to execute. To force the shell program to end immediately when an error occurs, you can use the-e option to terminate the execution of the shell program immediately.
(2) Shell program tracking (-v or-x)
The primary way to debug a shell program is to use the-V or-x option of the shell command interpreter to track program execution. The-V selection causes the shell to display every command line it reads into while executing the program, and the-X selection causes the shell to display each command it executes during the execution of the program at the beginning of the row with a plus command name. and displays the value of each variable and the variable. The main difference, therefore, is that without-V before the command line is executed, the original content of the command line is displayed, and a-V displays the contents of the replaced command line.
Example
./iftest.sh:
#! /bin/bashiftest () {if[$#-ne1] then echo"Usage: + dir"Exit1fi #Set-V orSet-x #若选择方法二, uncomment and select one of the set commandsif[-D" $"] then LS-L $1Exit0fi #Set+v orSet+x #若选择方法二, uncomment and select one of the set commands echo"warn:$1 is not a directory!"Exit2}
iftest .
Method One run Result:
Bash-v./iftest.sh
Bash-x./iftest.sh
Note: $#=1 $1=.
Method two running results:
Cancels the comment in the iftest.sh program, and selects only the Set-x and set +X commands, indicating only the second if: FI statement block for debugging.
./iftest.sh
Thank you: http://www.cnblogs.com/hicome/archive/2007/10/30/943002.html
Linux Shell Program Debugging