In shell, the simplest debugging assistant outputs the echo statement. Many echo statements can be put into the code for debugging, but it takes enough time to locate
The information to view. You may need to output a lot to find the information you want to search.
1. the most basic set-O Command Options are set-O Command Options. These options can be used on the command line when a script is run, as shown in the following table: set-O option command line option behavior noexec-N does not run the command, Value Check syntax error verbose-V before running the command to send them xtrace-x delivery for replacement after the command
The content of script a. Sh is: set-n
If CD/dev; do
Echo "in Dev execution script result: $./A. Sh
./A. sh: Line 2: syntax error near unexpected token 'do'
./A. sh: Line 2: 'If CD/dev; do'
D
The content of script a. Sh is:
Set-V
Echo "1"
Echo "2"
Run the Script: $./A. Sh
Echo "1"
1
Echo "2"
2
The content of script a. Sh is:
Set-x
Echo "$ PWD" execution script result: ++ echo/home/yanwenjie/bashtest
/Home/yanwenjie/bashtest
The-x option returns the command line result after parameter replacement, command replacement, and other command line processing steps. The X option prints + at the beginning of each line (but I don't know why I printed two). This can be customized. It is the value of the built-in shell variable PS4. You can modify this symbol by modifying the PS4 value. For example, the content of script a. Sh is as follows: PS4 = "debug->"
Set-x
Echo "$ PWD"
Execution result: $./A. Sh
Ddebug-> echo/home/yanwenjie/bashtest
/Home/yanwenjie/bashtest
(I don't know why two D S appear here .. Who can answer)
Let's briefly review the functions of PS1, ps2, and PS3: PS1 is used to control the style of shell prompts. For example, the pattern for logging on to the shell is [email protected]: ~ # PS2 is the multi-line prompt style when "\" is input. The default is ">". PS3 is the prompt when select is used in shell scripts.
2. the pseudo-signal and the working method are the same as trap, but they are generated by the shell itself, and can be treated like the actual signal in the shell script. When the pseudo-signal sending time exit shell exits from the script, the debug shell has executed a statement.
2.1.exit example: The following script a. sh: Trap "Echo 'exit from shell'" Exit
Echo "hello"
Execution result: #./A. Sh
Hello
Exit from Shell
2.2.debugdebug is used to execute trap code after all statements in a function or script. It is mainly used as a mandatory Method for tracking program State elements of errors. For example, the following script a. sh: function dbgtrap
{
Echo "I = $ I"
}
I = 1
Trap dbgtrap debug
I = $ (I + 1 ))
I = $ (I + 1 ))
I = $ (I + 1 ))
Trap-Debug
After each statement between two traps in the script is executed, the dbgtrap function is executed, including the first trap statement. Execution result: #./A. Sh
I = 1
I = 2
I = 3
I = 4
Where I = 1 is output after the trap dbgtrap debug statement is executed.