Summary of the For loop in the shell

Source: Internet
Author: User

This article mainly introduced the shell in the For loop summary, this article explained in the shell for the loop usage, the shell for the loop's several methods and so on content, needs the friend to be possible to refer to under

About the shell in the for loop usage a lot, always want to sum up, today on the Internet to see a summary of the use of the loop, feeling very comprehensive, so turn around research, HEI ...

The code is as follows:

For ((i=1;i<=10;i++));d o echo $ (expr $i * 4);d One

Used in the shell is for I in $ (seq 10)

The code is as follows:

For i in ' ls '

For i in ${arr[@]}

For i in $*; Todo

For File in/proc/sys/net/ipv4/confaccept_redirects: '

For File in/proc/sys/net/ipv4/conf/*/accept_redirects; Todo

Echo $File

Done

echo "Specify loop content directly"

For I in F1 F2 F3;d o

Echo $i

Done

Echo

echo "C syntax for loop:"

for ((i=0; i<10; i++)); Todo

Echo $i

Done

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

For-loop usage in the shell

Shell syntax Good trouble, a loop has been done for a while, and found several different ways to achieve output 1-100 can be divisible by 3 number

1. Use (())

The code is as follows:

#!/bin/bash

Clear

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

For

Todo

if ((i%3==0))

Then

Echo $i

Continue

Fi

Done

2. Use ' SEQ 100 '

The code is as follows:

#!/bin/bash

Clear

For i in ' SEQ 100 '

Todo

if ((i%3==0))

Then

Echo $i

Continue

Fi

Done

3. Use while

The code is as follows:

#!/bin/bash

Clear

I=1

while (($i <100))

Todo

if (($i%3==0))

Then

Echo $i

Fi

i=$ (($i + 1))

Done

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

When the shell uses the for loop to increase the number of digits to discover the problem, it lists several methods of the shell for the loop:

1.

Copy code code as follows:

For i in ' seq 1 1000000 ';d o

Echo $i

Done

With the SEQ 1 10000000 increment, before using this method did not encounter problems, because the previous I was useless to millions (1000000), because the project needs me this number is far greater than million, found in SEQ value to 1000000 when converted to 1e+06, Can not be used as a number of other operations, or the $i effective, correct access, and then find other ways to solve, as follows

2.

The code is as follows:

For ((i=1;i<10000000;i++));d o

Echo $i

Done

3.

I=1

while (($i <10000000));d o

Echo $i

i= ' expr $i + 1 '

Done

Because this method calls expr, it will run faster than the 1th, 2nd, but can be slightly improved, will be i= ' expr $i + 1 ' to i=$ (($i + 1)) can be a little speed, but specifically to see if the corresponding Shell environment support

4.

Copy code code as follows:

For I in {1..10000000;do

Echo $i

Done

In fact, choose which method is specific or by the corresponding Shell environment support, to achieve the desired effect, and then consider the problem of speed.

Example:

The code is as follows:

#!/bin/sh

I=1

function Test_while () {

I=1

While [$i]

Todo

Echo $i

i= ' expr $i + 1 '

If [$i-ge 10]; Then

Break

Fi

Done

}

function Test_for () {

I=1

for ((I=1; i<=100; i++)); Todo

Echo $i

If [$i-ge 10]; Then

Break

Fi

Done

}

function Test_continue () {

I=1

For I in $ (seq 100); Todo

if ((i==0)); Then

Echo $i

Continue

Fi

Done

}

echo "Test_while ..."

Test_while

echo "Test_for ..."

Test_for

echo "Test_continue ..."

Test_continue

Run Result:

The code is as follows:

Test_while ...

1

2

3

4

5

6

7

8

9

Test_for ...

1

2

3

4

5

6

7

8

9

10

Test_continue ...

10

20

30

40

50

60

70

80

90

100

Note < > : More Wonderful tutorials please focus on Triple programming

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.