Learning bash notes-debugging shell programs and learning bash shell

Source: Internet
Author: User

Learning bash notes-debugging shell programs and learning bash shell

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. 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.
How to run and debug shell programs?

1. Run the command sh test. sh;
This file can be run with executable permissions:
Chmod u + x test. sh
./Test. sh

Debugging: add an output statement such as echo to determine where necessary.

How can I debug shell scripts? Various methods

Method 1: sh-x script. sh
The-x option displays the script content on the screen with a + sign in front of it. In this way, you will know which sentence has been executed. It is helpful for debugging.

Method 2: Use the debug switch in the script
This method is applicable to debugging only some scripts.
Set-x
... (Code segment to be debugged)
Set + x
Then run the script sh script. sh.

Method 3: Add printing to the script
For example, a log output function is used to print debugging information.
_ Log ()
{
If ["$ _ DEBUG" = "true"]; then
Echo "debugging information"
Fi
}
You only need to control the $ _ DEBUG switch.

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.