Summary of the for Loop in Shell

Source: Internet
Author: User

Summary of the for Loop in Shell

This article mainly introduces the for loop summary in Shell. This article describes the usage of for loop in shell and several methods of for loop in shell. for more information, see

There are a lot of for loop usage in shell. I always want to summarize it. Today I saw a summary of for loop usage on the Internet, which is very comprehensive. So I will turn to research and study...

The Code is as follows:

For (I = 1; I <= 10; I ++); do echo $ (expr $ I \ * 4); done

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

The Code is as follows:

For I in 'LS'

For I in $ {arr [@]}

For I in $ *; do

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

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

Echo $ File

Done

Echo "specify loop content directly"

For I in f1 f2 f3; do

Echo $ I

Done

Echo

Echo "C syntax for loop :"

For (I = 0; I <10; I ++); do

Echo $ I

Done

Bytes ---------------------------------------------------------------------------------------------------------

For Loop usage in shell

Shell syntax is very troublesome. A loop is completed for a while. I found several different methods to implement 3 divisible numbers between 1 and output records.

1. Use (())

The Code is as follows:

#! /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'

The Code is as follows:

#! /Bin/bash

Clear

For I in 'seq 100'

Do

If (I % 3 = 0 ))

Then

Echo $ I

Continue

Fi

Done

3. Use the while

The Code is as follows:

#! /Bin/bash

Clear

I = 1

While ($ I <100 ))

Do

If ($ I % 3 = 0 ))

Then

Echo $ I

Fi

I = $ ($ I + 1 ))

Done

Bytes --------------------------------------------------------------------------------------------------------

When the shell uses the for loop to Increase the number, it finds the problem. It lists several methods for the for Loop in shell:

1.

Copy the Code as follows:

For I in 'seq 1 1000000 '; do

Echo $ I

Done

I used seq 1 10000000 to increase progressively. I didn't encounter any problems when I used this method, because I didn't use millions (1000000) at all, because the project requires me to have a larger number than one million, it is found that when the seq value is used to 1000000, it is converted to 1e + 06, and it cannot be used as a number for other operations, or $ I can be effectively and correctly used, and other methods are solved as follows:

2.

The Code is as follows:

For (I = 1; I <10000000; I ++); do

Echo $ I

Done

3.

I = 1

While ($ I <10000000); do

Echo $ I

I = 'expr $ I + 1'

Done

Because this method calls expr, the running speed is much slower than 1st and 2nd, but it can be slightly improved, change I = 'expr $ I + 1' to I = $ ($ I + 1) to slightly increase the speed, but it depends on whether the corresponding shell Environment supports

4.

Copy the Code as follows:

For I in {1 .. 10000000; do

Echo $ I

Done

In fact, the method used must be supported by the corresponding shell environment to achieve the expected results, and the speed issue should be considered.

Example:

The Code is as follows:

#! /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

Running 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 <>: For more exciting tutorials, please pay attention to the help house programming

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.