Use shell scripts to control conditions and use loops.

Source: Internet
Author: User

Use shell scripts to control conditions and use loops.

Reprinted please mark: http://www.cnblogs.com/winifred-tang94/

If Condition Statement Syntax:

If [conditional expression]

Then

Code

Else

Code

Fi

Note:: In the preceding if Condition Statement, there must be spaces between if and [, while both conditional expressions and [] must have spaces. The conditional expressions cannot be close to [and].

If condition expression ends with fi.

Eg.

The following problem occurs:

The reason is that it starts #! Then there is a/, and the/Interpreter error is gone. The Code is as follows:

But there is still no error in that file or directory. For this error, I think there is a problem with the echo expression. So I deregistered the expression and output small and bigger. The result is that the program runs correctly and the output is small or bigger.

So for the expression after echo, I add double quotation marks to the expression, and the result runs successfully. No problem is found.

Use the case expression for conditional judgment:

Case variable in

Str1)

Command string ;;

Str2)

Command string ;;

*)

Eg.

The error occurred because the Code does not end correctly. The case Condition Statement should end with esca in shell.

The following error occurs:

Cause:

*) Is a Chinese character, and it is changed to an English character. The end of the case condition statement should be esca rather than easc. After modification, it is as follows:

The correct result is as follows:

Use the for Loop in shell scripts

Three types of loops can be used in shell:

A. for Loop

The first syntax format:
For var in list

Do

Command string

Done

Note that the preceding list format can be $1 $2 $3 separated by space, or the value of 1 2 3 separated by space.

The second syntax format:

For var

Do

Command string

Done

In this form, list is omitted. In this case, list refers to the command line parameter, and the var of each loop is a command line parameter.

Eg. Sum of output command line parameters

Note:In the preceding shell program, the echo Command Parameter-e is used to make \ c and \ B take effect, where \ c indicates that no line breaks are generated after the output number is added, \ B Indicates deleting the excess number +.

An error occurs after the script is run:

Cause:

Expr cannot use the-gt command of the test command after marriage. Otherwise, change expr to test, or change-gt to> =

, It is worth noting that> = is used before \.

Still error:

The reason is:

When sum is used,There cannot be spaces at both ends of the equal sign.

Changed:

The result is:

B. while Loop

Syntax format:
While condition expression

Do

Command Line string;

Done

Eg. output each parameter in the command line

Shift command shifts the parameter of the command line to the left, that is, the parameter in the second place is changed to the first and then output.

Result:

Note:The while LOOP condition expression also uses spaces to separate the condition expression from [and.

If you want to use another variable n in the while clause, The unary expression used in the loop body should be expressed in the form of-n:

Of course, if-n is not added to the while clause, the result is the same:

C. until Loop

The syntax format is as follows:

Until conditional expression

Do

Command string;

Done

 

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.