Linux Shell six flow control--loop

Source: Internet
Author: User

One, for loop

The For loop works by taking the serial element one by one out, putting it into the specified variable sequentially, and repeating the contained command area (between do and done) until all the elements take

Until the end. Where serial is a combination of strings, separated from each other by $ifs-defined segmentation characters, such as spaces, which are called fields.

For syntax

The for variable in serial iterates over the serial field into the variable

Do

Command area repeatedly executes the command area, knowing that each field in the serial is processed

Done

!/bin/bash

For I in 1 2 3 4

Do

Echo $i

Done

--------------------------------------------------------------

For variable in [email protected][email protected] represents all parameters of the command line

Do

Command Area

Done


#!/bin/bash

Ifs= ': ' For loops are often used in splitting string rows, removing field element values

Pl= "Root:x:0:0:root:/root:/bin/bash"

For I in $PL

Do

echo $i result is to output each field sequentially

Done


#!/bin/bash

Dir= "/root"

For I in $ (LS $DIR)

Do

echo "The Files is $i"

Done


#!/bin/bash

Dir= "/etc"

CD $DIR

For I in $ (LS $DIR)

Do

[-D $i]&& du-s $i If it is a directory, output its size

Done

--------------------------------------------------------------

for (initial conditions; termination criteria; abnormal items))

Do

Command Area

Done

!/bin/bash

declare-i I sum declaration variable is an integer

for ((I=1; i<=10; i=i+1)

Do

Let sum+=i sum increases the value of I every time sum=sum+i

Done

The Echo $sum value is 1+2+: +10=55

--------------------------------------------------------------

Infinite loop

for ((; 1;))

Do

Command

Done

#!/bin/bash

for ((; 1;))

Do

echo "Hello" screen infinite output fuck,ctrl+c end

Done

Second, while loop

The syntax of the while

While condition test return value is true, enter command, otherwise end script

Do

Command area These commands should have a command to change the condition test, so that you have the opportunity to end the while loop after a finite step (unless you want to perform an infinite loop).

Done

!/bin/bash

Declare-i I=1 declaration I and sum are integer type

Declare-i sum=0

while ((i<=10)) I loop as long as it is less than or equal to 10

Do

Let Sum+=i sum+=i and Sum=sum+i are the same, sum accumulates on I

Let ++i i values increment by 1, a single greater than 10 stop loop

Done

Echo $sum

--------------------------------------------------------------

#!/bin/bash

Ifs= ': ' delimited character variable value is ': '

While read F1 F2 F3 f4 f5 f6 F7 read/etc/passwd line account data and put in F1~F7

Do

echo "Account: $f 1,login Shell is: $f 7" Displays the name of each account and the login shell it uses

Done < "/etc/passwd" to read the/etc/passwd data by turning

--------------------------------------------------------------

#/bin/bash

Ifs= ': '

cat/etc/passwd | {cat/etc/passwd Throwing instructions to the {} GROUP by pipe

While read F1 F2 F3 f4 f5 f6 F7

echo "account is: $f 1,login shell is $f7"

Done

}

--------------------------------------------------------------

While Infinite loop

#!/bin/bash

while ((1)) while[1] or while: or while true

Do

echo "HI"

Done

Third, until cycle

The condition of the while loop is the measured value, and the Until loop is the false values.

Until syntax

Until condition test if the condition test result is false (return value is not 0), enter the loop

Do

Command area These commands should have the command to change the condition test so that you have an opportunity to end the unitl loop after a finite step (unless you want to perform an infinite loop)

Done back to the beginning, execute the until command

!/bin/bash

Declare-i I=1

Declare-i sum=0

Until ((i>10)) If I is not greater than 10, enter the loop

Do

Let Sum+=i

Let ++i

Done

Echo $sum

---------------------------------------------------

Until Infinite loop

!/bin/bash

Until ((0)) or until false

Do

echo "Fuck"

Done

Iv. Select command

The Select command can create a simple list, with syntax and for

Select syntax

Select option variable in serial

Do

Command Area

Done

It is generally used in conjunction with functions to create a visual list.

V. Break and Continue

If you want to end the loop early, you can use break in the loop, a break will jump out of the loop, and if you want to jump out of the multilayer, you can add layer n after the break command (n>=1)

#!/bin/bash

For ((i=1;i<=10;i++))

Do

[$is-eq 6];then If the value of I is 6, jump out of the loop

Break

Fi

Echo $i

Done

---------------------------------------------------

#!/bin/bash

For ((i=1;i<=10;i++))

Do

For ((j=1;j<=10;j++))

Do

r=$ ((I+J))

If [$r-eq];then when I and J and is 19, jump out of the 2-layer loop

Break 2

Fi

Echo $r

Done

Done

The continue command skips this cycle and starts execution again from the next loop.

#!/bin/bash

For ((I=1;i<=10;++i))

Do

If [$i-eq 6];then

Continue

Fi

Echo $i

Done

When the value of I equals 6, it jumps out of the loop (the following command is no longer executed, echo $i), and then starts again from the next loop, so the upper example shows the 7~10, and only 6 does not show, because 6 jumps out and then executes again. If there is a multilayer loop, you can specify which layer of loop to restart execution after continue. (for example, continue2 means jumping out of a 2-layer loop from inside, and then starting with the cycle of that layer).

This article is from the "Welcome to Linux World" blog, so be sure to keep this source http://linuxnote.blog.51cto.com/9876511/1641206

Linux Shell six flow control--loop

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.