Shell Programming (iv)

Source: Internet
Author: User

In this article we learned about the use of conditional selection statements in the shell. The next step is to learn the looping statements. In the shell, loops are implemented through the for, while, until commands. Let's take a look at each one below.

For

There are two types of for loops:

for-in statements

The basic format is as follows:

 for var inch  do    commandsdone

 

The list represents the value to be looped, and at each iteration, the current value is assigned to VAR (variable name, arbitrary) so that the current value can be obtained directly through $var in the loop body.

Let's start with an example:

#!/bin/bash for in a b c d edo    echo $strdone    

 

The above will split the ABCDE according to the space, then output it sequentially.

If the above example is not separated by a space, but separated by a comma (,)?

#!/bin/bashlist="a,b,c,d,e" for in $list  do     echo $strdone

 

Result output A,b,c,d,e

The reason for this result is that the for...in loop is the default loop for a set of values that are split by a Space or tab (tab) or newline (enter key). This is actually configured by the internal field delimiter , which is defined by the system environment variable IFS. Of course, since it is defined by the environment variable, it can also be modified.

Modify IFS values
#!/bin/bash# defines a variable oldifs save the value of the IFS before it is modified oldifs=$IFS # Modify the IFS value, comma-delimited ifs=$','List=A,b,c,d,elist2="a b c d e" for var inch$list Doecho $var Done forVar2inch$list 2 Doecho $var 2done# to restore IFS value IFS= $oldIFS

 

The first loop above will output ABCDE several values respectively. The second loop outputs a B c D e (that is, unhandled). Because we set the value of IFS to a comma, of course, not necessarily a comma, want to set what, you decide!

C language-style for loop

The C language-style for loop in Bash follows the following format:

for ((variable assignment; condition; iteration process))

An example is sufficient to illustrate:

#!/bin/bashfor0; i++ ))do    echo $idone  

The above example loops 11 times, outputting from 0 to 10 in turn. A little bit of programming basics should be familiar. Do not elaborate on the details.

While loop

If you get used to the while loop in other languages, then you'll find this while loop a bit perverted. Unlike other programming languages, the while statement in Bash appears to be a combination of the If-then statement (refer to the previous article) and the FOR Loop statement. Its basic format is as follows:

while doing other    commandsdone 

 

As with the If-then statement, follow the test command, if the exit status code for the command after test is 0. Then go into the loop and execute the logic behind do. Be aware of the write condition in the logic behind do to avoid a dead loop.

Since it is the test command, then everything can refer to If-then's test

Example one:

#!/bin/bashflag=0    while doing echo $flag    # If there's no such thing, Then flag has a value of 0, and    the flag1]done is executed indefinitely.

 

The above determines if the flag is greater than or equal to 10, if the condition is met, then the value of the current flag is output, and then the value of flag is added 1. The result of the final output is a result of 0 to 10.

In combination with the previous article test, we can also deform the above example as follows:

Example two:

#!/bin/bashflag=0 while )do    echo $flag    flag  1]done

 

Example three:

flag=0 ) )do    echo $flag    flag1] Done

 
Until loop statements

The basic format of the until statement is as follows:

until test commands  Do Other     Commandsdone

 

After mastering the While Loop statement, the until statement is simple. The until statement is exactly the opposite of the while statement, which executes the loop when the test command exits the status code of 0, while the until statement executes when the test command exit status code is not 0.

Example:

#!/bin/bashflag=0  )do    echo $flag    flag 1 ]done

 

Output values from 0 to 10 above. The condition behind the until is the exact opposite of the above while example.

Well, here we have learned the Shell's loop statement. But the loop statements we wrote above are all executed according to the conditions, what if we want to quit during the execution? The next step is to see how to control the loop statement.

Control loops

Like other programming languages, the shell controls loops through the break and continue commands. Here's a look at the basic usage of the two:

Break
    1. Break is used to jump out of the current loop.

Example one:

#!/bin/bash for ((flag=0, flag++ ))do    if5 ) then break    fi    echo $flagdone         

 

Above when the value of flag is 5, exit the loop. The value of the output is 0-4.

    1. Break is used to jump out of the inner layer loop .

Example two:

#!/bin/Bashflag=0 while(($flag <Ten )) Do     for((innerflag=0; Innerflag <5; innerflag++ ))     Do        if($innerFlag = =2) Then Breakfi echo"innerflag= $innerFlag"Done Echo"outerflag= $flag" Done

 

The above code in the execution of the internal loop for, when the Innerflag value of 2 will jump out of the outer while loop, because the outer loop has been flag is 0, so the while will become a dead loop, non-stop output:

...

Innerflag=0

Innerflag=1

Outerflag=0

...

    1. Break to jump out of outer loop

Break can be followed by a number that represents the layer loop that exits the outer part of the current loop.

Example three:

#!/bin/Bashflag=0 while(($flag <Ten )) Do     for((innerflag=0; Innerflag <5; innerflag++ ))     Do        if($innerFlag = =2) Then # 2 indicates a layer of loop outside Break 2fi echo"innerflag= $innerFlag"Done Echo"outerflag= $flag" Done

 

In contrast to the above example, this example simply follows a break followed by a number 2, which indicates exiting the first layer of the loop. Final output:

Innerflag=0

Innerflag=1

Continue

Continue means terminating the current loop, entering the next loop, and note that the statement following the continue will not execute.

Continue syntax is the same as break, so just one example is shown.

Example:

flag=0 while(($flag <=Ten )) Do    if($flag = =5) then flag=$[$flag +1]        Continuefi echo"outerflag= $flag"     for((innerflag= One; Innerflag < -; innerflag++ ))     Do        if($innerFlag = = -) then flag=$[$flag +1]            Continue 2fi echo"innerflag= $innerFlag"Donedone

 

Above example: When the value of Innerflag in the For loop is 16, it jumps to the outer while loop, and when the value of the outer loop flag is 5, it skips the loop and then goes to the next loop, so there is no outerflag=5 in the output result.

Shell Programming (iv)

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.