Method One: Add debugging options when you run the script:
"Usage" bash– option./shellscript.sh
-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
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
#! /bin/bash
Iftest () {
If [$#-ne 1]
Then
echo "Usage: + dir >"
Exit 1
Fi
# set-v or Set-x #若选择方法二, uncomment and select one of the set commands
If [-d ' >]
Then
Ls-l $
Exit 0
Fi
# set +V or set +x #若选择方法二, uncomment and select one of the set commands
echo "Warn:$1 is not a directory! > "
Exit 2
}
Iftest.
Linux Shell Program Debugging