The For loop summary in the shell _linux shell

Source: Internet
Author: User

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 ...

Copy Code code 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)
Copy Code code 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 (())

Copy Code code 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 '
Copy Code code as follows:

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

3. Use while
Copy Code code 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.
Copy Code code 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:
Copy Code code 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:
Copy Code code 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

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.