(029) Linux Shell fault Diagnosis

Source: Internet
Author: User
Tags echo command

Fundamentals of the ten-year OPS series-Linux

Zeng Lin

Contact: [Email protected]

Website: www.jplatformx.com

Copyright: Please do not reprint the article without permission

First, Introduction

As scripts become more complex, you need to see where the problem is when the script is wrong or the execution and expectations are different. This chapter explains some of the common error types in scripts and several useful tips for tracking and releasing errors.

Second, grammatical errors

Syntax errors are a common type of error, including spelling errors in some elements of a shell statement. In most cases, the shell refuses to execute a script that contains this type of error.

During the next discussion, we will use the following script (foo.sh) to demonstrate common error types. The following code is specific:

#!/bin/bash# Foo:script to demonstrate common errorsnumber=1if1 ];  then    Echo " Number is equal to 1. " Else    Echo " Number is not equal 1. " fi Exit

This script runs without errors and works as expected. The results of the operation are as follows:

  

1. Missing quotation marks

Now modify the above script to delete the double quotation marks after the first echo command after the argument. Specific example:

#!/bin/bash# Foo:script to demonstrate common errorsnumber=1if1 ];  then     echo"number isequal to 1.  Else    Echo " Number is not equal 1. " fi Exit

The results after the run are as follows:

This delete action causes the script to generate two errors. Interestingly, the error report indicates that the row is not the same as the row where the double quotation marks were deleted, but the code that follows. As you can see, when the system reads the position of the deleted double quotation marks, bash continues looking down at the quotation marks corresponding to the preceding double quotes, which continues until the bash finds the target, which is the first quotation mark after the second echo command. Bash then gets into a mess where the syntax structure of the IF command is broken, and the FI statement is now in a string that is marked with quotation marks (but only one side exists).

This type of error is difficult to find in long scripts, and using an editor with syntax structure highlighting can help find such errors. If the system is equipped with a full version of VIM, use the following command to enable the syntax structure highlighting feature of Vim. The syntax is as follows:

  : Syntax on

2. Missing symbols

Another common mistake is that compound command structures such as if or while are incomplete. Let's say we delete the semicolon from the test section of the If command in the script above and see what happens. The code is as follows:

#!/bin/bash# Foo:script to demonstrate common errorsnumber=1if1Then     echo"number isequal to 1. " Else    Echo " Number is not equal 1. " fi Exit

The execution results are as follows:

  

(029) Linux Shell fault Diagnosis

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.