Note points that require special attention in writing shell scripts

Source: Internet
Author: User

    1. Conditional Judgments in shell scripts

1.

if [Condition];then

Statement

Fi

2.

If [Condition];then

Statement

elif [Condition];then

Statement

Else

Statement

Fi

Note the point:

1-1. If and fi are paired, a syntax error occurs when the script executes when the fi is missing.

1-2. When the condition is judged, the use of square brackets requires special care, where there must be a space between if and [, there must be a space between the front bracket and the conditional expression,] there must be a space between the back bracket and the conditional expression, which requires attention to these three places.

Example: The script shown in 1, when there is no space between if and [

Figure 1

Its running result 2

Figure 2

Thus, the treatment of square brackets "[]" must be taken seriously.

Loops in a 2.shell script

2-1.for Loop Statements

#!/bin/bash

For x in one, three four

Do

Echo Number $x

Done

Attention:

In this loop, the value of each variable x is a type of value in the list after "in", which can be a file name

2-2.while Loop Statements

#!/bin/bash

Var=1

While [$var-le 10]

Do

Echo $var

var=$ (($var + 1))

Done

Note: It is necessary to determine whether the condition is true before the loop executes.

2-3.until Loop Statements

#!/bin/bash

Var=1

Until [$var-GT 10]

Do

Echo $var

var=$ (($myvar + 1))

Done

The method of self-increment of five kinds of variables

1. i= ' expr $i + 1 ';

2. Let i+=1;

3. ((i++));

4. i=$[$i +1];

5. i=$ (($i + 1))

Note points that require special attention in writing shell scripts

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.