Chapter 6 of linuxshell programming guide ------ Script Debugging

Source: Internet
Author: User
One of the most annoying tasks of shell programming is debugging. Some methods can be used for reference, but it is best to prevent most errors before the problem occurs. Therefore, follow the following rules. One thing to remember is that when the shell prints a script error, do not just look at the rows in question.

One of the most annoying tasks of shell programming is debugging. Some methods can be used for reference, but it is best to prevent most errors before the problem occurs. Therefore, follow the following rules.

One thing to remember is that when the shell prints a script error, do not just look at the rows in question. Instead, observe the entire code segment. Shell does not precisely locate errors, but performs error statistics when trying to end a statement.

Loop error

Errors in for, while, until, and case statements indicate that the actual statement segment is incorrect. A reserved word in a fixed structure may be missing.

The following error message done is a good clue. This is because a while statement is being processed. Backtrack the script segment and check whether keywords, such as do or condition statements in use, are missing or mistakenly written in the while statement.

The second typical error is missing quotation marks. Pay attention to this problem because it often occurs. The only solution to this type of error is to ensure that all quotation marks appear in pairs in the script.

After the shell prints an error line, you can view the file in the vi editor. Debug the error by using the set nu option of vi. first enter vi, then click the <ESC> Key, followed by a colon, and then type set nu. In this case, the text line number is given and the shell prints the error line.

Another common error is that you forget to use a numeric value on one side of the test condition when using the-eq statement.

If the following error message is displayed, it is usually caused by two things: a space must be added between the variable and square brackets, and an operator is missing in square brackets.
[: Missing ']'

In terms of experience, most errors are caused by inconsistent big and lowercase variables. For example, you often use uppercase letters at the beginning of definition and lower-case characters at the time of variable calling. in this way, the variable will not be assigned a value.

When using a for loop, you sometimes forget to use the $ symbol in the loop list, especially when reading strings.

The set command can assist in script debugging. The following are common debugging options for the set command:

The set-n read command is not executed.

Set-v displays all rows read.

Set-x displays all commands and their parameters.

Turn off the s e t option and replace-with + -. Some people think that + should be enabled, but-should be disabled, but the opposite is true. You can enable the s e t option at the beginning of the script and disable it at the end of the script. Or open or close a special statement segment that is considered problematic.

The following is an example. The following script saves the name in the variable list. Enter the name and check whether the matching mode exists in the for loop variable list. Note that set-x is used at the beginning of the script and is disabled at the end.

[Root @ localhost huangcd] # cat error
#! /Bin/bash
Set-x
LIST = "Peter Susan John Barry Lucy Norman Bill Leslie"
Echo-n "Enter your Name :"
Read NAME
For LOOP in $ LIST
Do
If ["$ LOOP" = "$ NAME"]
Then
Echo "you are on the list ,"
Break
Fi
Done
Set + x
[Root @ localhost huangcd] # sh error
+ LIST = 'Peter Susan John Barry Lucy Norman Bill Leslie'
+ Echo-n 'Enter your Name :'
Enter your Name: + read NAME
Lucy
+ For LOOP in '$ list'
+ '['Peter = Lucy']'
+ For LOOP in '$ list'
+ '['Susan = Lucy']'
+ For LOOP in '$ list'
+ '['John = Lucy']'
+ For LOOP in '$ list'
+ '['Barry = Lucy']'
+ For LOOP in '$ list'
+ '['Lucy = Lucy']'
+ Echo 'you are on the list ,'
You are on the list,
+ Break
+ Set + x

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.