In-Shell for loop

Source: Internet
Author: User

About the shell for the use of a lot of usage, has been trying to summarize, today on the Internet to see a summary of the use of the loop, feel very comprehensive, so turn around research, hey ...

1, for ((i=1;i<=10;i++));d o echo $ (expr $i \* 4);d One
2. Common in Shell is for I in $ (seq 10)
3, for i in ' ls '

4, for I in ${arr[@]}
5, for I in $*; Do
6. For File in/proc/sys/net/ipv4/confaccept_redirects: '
18.for File in/proc/sys/net/ipv4/conf/*/accept_redirects; Do
19.echo $File
20.done
21.echo "Specify circular content directly"
22.for i in F1 f2 F3;d o
23.echo $i
24.done
25.echo
26.echo "C syntax for loop:"
27.for ((i=0; i<10; i++)); Do
28.echo $i
29.done

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

Shell in for loop usage
The shell syntax is troublesome, a loop has been made for a while, and several different methods have been found to achieve the number of outputs 1-100 divisible by 3
1. using (())
#!/bin/bash
Clear

For ((i=1;i<100;i++))
For
Do
if ((i%3==0))
Then
Echo $i
Continue
Fi
Done
2. Use ' SEQ 100 '
#!/bin/bash
Clear

For i in ' SEQ 100 '
Do
if ((i%3==0))
Then
Echo $i
Continue
Fi
Done
3. Using the While
#!/bin/bash
Clear

I=1
while (($i <100))
Do
if (($i%3==0))
Then
Echo $i
Fi
i=$ (($i + 1))
Done



--------------------------------------------------------------------------------------------------------
When the shell uses the for loop to make the number increment, it finds a problem, and lists several methods for the shell for Loop:

1.

For i in ' seq 1 1000000 ';d o

Echo $i

Done

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

2.

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 so the speed will be slower than the 1th, 2nd, but can be slightly improved, the i= ' expr $i + 1 ' to i=$ (($i + 1)) can be slightly increased speed, but depends on the appropriate shell environment support

4.

For I in {1..10000000;do

Echo $i

Done

In fact, the choice of which method specific or to be supported by the corresponding shell environment, to achieve the desired results, and then consider the problem of speed.

Example:

#!/bin/sh

I=1

function Test_while () {

I=1

While [$i]

Do

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++)); Do

Echo $i

If [$i-ge 10]; Then

Break

Fi

Done

}

function Test_continue () {

I=1

For I in $ (seq 100); Do

if ((i==0)); Then

Echo $i

Continue

Fi

Done

}

echo "Test_while ..."

Test_while

echo "Test_for ..."

Test_for

echo "Test_continue ..."

Test_continue

Operation Result:

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

In-Shell for 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.