Analysis and application of if,for,while in shell script of Linux

Source: Internet
Author: User

LInux's Shell has a set of its own flow control statements, including conditional statements (IF), loop statements (For,while), which have a great effect on our usual scripting and command input, which will certainly help us a lot.

I. IF

If is a conditional judgment statement, its statement structure is as follows

The If [condition];then # in parentheses is a conditional test statement, indicating that if the condition is true, then the contents after then are executed

Action #条件为真时需要执行的动作

elif [Condition];then #执行完上一条的动作后, continue to judge the conditions after the elif, if True then perform the next action

Action #elif条件为真的时候执行的动作

Else # if the condition after Elif is false (can not, and continue a new elif judgment)

Action #若elif后的条件为假时, Actions performed

else #若if的条件为假的时候

Action #if的条件为假时候, Actions performed

Fi #判断结束

Note: (After the end of the conditional test statement) should be preceded by a semicolon, of course, can not add, and then to write to the next line, the individual prefer to write to a line, so it seems more neat specification)


The following is an example of a script that uses if to determine whether the year entered by the user is leap years


650) this.width=650; parsing and application of if,for,while in shell scripts for "alt=" Linux-m13526593315-m13526593315 Blog "src="/HTTP// Img0.ph.126.net/ssshbbo_j04pq2wskwe01a==/6632620276117082526.png "style=" Line-height:28px;border:0px;height: auto;margin:0px 10px 0px 0px; "/>

Let's look at a script example of if judging score


650) this.width=650; parsing and application of if,for,while in shell scripts for "alt=" Linux-m13526593315-m13526593315 Blog "src="/HTTP// Img2.ph.126.net/msrzcwn2zpinsxxpu49bkw==/6632426762073195506.png "style=" Line-height:28px;border:0px;height: auto;margin:0px 10px 0px 0px; "/>

 

Two. For

For is a looping structure, and its basic structure is as follows:

For variable in seq string #循环的字符串

Do #循环开始

Action #循环的内容

Done #循环结束

Note: (Circular string can be expressed in two ways, one is similar to the C language, but to add a double-sided parentheses, such as ((i=0;i<=100;i++)), the natural is that I add 1 each time, until more than 100 to terminate the loop, The other is an array of variables plus in plus variables, such as I in ' ls ', which means that the variables listed in the LS command are assigned to I once.

Let's look at A For loop calculation 1: 100 and the number of the script


650) this.width=650; parsing and application of if,for,while in shell scripts for "alt=" Linux-m13526593315-m13526593315 Blog "src="/HTTP// Img0.ph.126.net/g3bkywlewmu_bapjdwg8iq==/6632404771840631406.png "style=" Line-height:28px;border:0px;height: auto;margin:0px 10px 0px 0px; "/>

Then write this question out in the form of another for loop.


650) this.width=650; parsing and application of if,for,while in shell scripts for "alt=" Linux-m13526593315-m13526593315 Blog "src="/HTTP// Img2.ph.126.net/qn-g9mxk3jc2vqb4ngcb8g==/6632447652794124527.png "style=" Line-height:28px;border:0px;height: auto;margin:0px 10px 0px 0px; "/>

 

Three. While loop

While is also a loop body, its basic structure is as follows:

While [condition] #循环的条件

Do #循环开始的标志

Action #循环的动作

Done #循环结束的标志

Let's look at a 10 to 1 countdown with a while loop.


650) this.width=650; parsing and application of if,for,while in shell scripts for "alt=" Linux-m13526593315-m13526593315 Blog "src="/HTTP// Img1.ph.126.net/se2yod582hhznqjwqsqjjw==/6632419065491804857.png "style=" Line-height:28px;border:0px;height: auto;margin:0px 10px 0px 0px; "/>

Finally, take a few examples of using for,while,if together


1. Let the user guess a number between 1-66, if not, prompt the user is too big or too small, until the user loses the pair for 30


650) this.width=650; parsing and application of if,for,while in shell scripts for "alt=" Linux-m13526593315-m13526593315 Blog "src="/HTTP// Img1.ph.126.net/_afdl3jq5dkm4xk8wdrqlw==/6632381682096431590.png "style=" Line-height:28px;border:0px;height: auto;margin:0px 10px 0px 0px; "/>

Let's take a look at the specific writing idea of this script, first let the user enter a number, where the Read command is used, the Read command is to let the user enter a parameter, you can use the-p option to prompt the user to enter the content, such as read-p "please input a count (0-66): "Count, this is also possible to write, and then use a while loop, when the user entered the number of the condition that the user entered the number is 30, the end of the loop, and output congratulations, you are right, otherwise do the contents of do, first if the argument is greater than 30, If it is the prompt output number is too large, and let the user re-enter, otherwise determine whether the parameter is less than 30, if the user is prompted by the number is too small oh ah, and let the user re-enter, and then fi End If statement, plus done to indicate the end of the while loop.

2. Add multiple user scripts at once


650) this.width=650; parsing and application of if,for,while in shell scripts for "alt=" Linux-m13526593315-m13526593315 Blog "src="/HTTP// Img0.ph.126.net/51ctnqck9zxlxwlnsjp1hg==/6632753317024050201.png "style=" Line-height:28px;border:0px;height: auto;margin:0px 10px 0px 0px; "/>

The first line of SEQ indicates that the preceding variable number will be looped between 1 and $ $, which is a positional variable that the user manually entered when executing the script and how many users need to be added, and do start the loop body, using the ID command to determine user$ Whether the user in number is already present, prompts the user if it exists, or executes the Add User's command, then done

3. Prompt the user to enter a number n, and calculate 1 to N and with the flight after output to the terminal


650) this.width=650; parsing and application of if,for,while in shell scripts for "alt=" Linux-m13526593315-m13526593315 Blog "src="/HTTP// Img2.ph.126.net/mwmiufdrk8j5hlwhoiez4w==/6632460846933656792.png "style=" Line-height:28px;border:0px;height: auto;margin:0px 10px 0px 0px; "/>

First define two variables sum1,sum2 and the sum of the product, and then prompt the user to enter a range to calculate, that is, the number, then determine whether the number is less than 1, if less than 1 prompts the user is too small, in the input of a larger number, if greater than 1, Executes the contents of the For loop after else, that is, the sum of all the numbers from 1 to n is calculated and multiplied and, finally, the end of the fi,if loop, the sum of the outputs added and the number multiplied by.



This article is from the "11083860" blog, please be sure to keep this source http://11093860.blog.51cto.com/11083860/1965785

Analysis and application of if,for,while in shell script of Linux

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.