Learning bash notes-debugging shell programs

Source: Internet
Author: User

Learning bash notes-debugging shell programs

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: root @ ywjPC :~ # 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. After the pseudo-signal sending time EXIT shell exits from the script, the DEBUG shell has executed a statement 2.1.EXIT. For example, the following script a. sh: trap "echo 'exit from shell'" exit
Echo "hello"
Execution result: #./a. sh
Hello
Exit from shell2.2.DEBUGDEBUG is used to execute the 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.