Summary of the for Loop in shell

Source: Internet
Author: User

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...
1. for (I = 1; I <= 10; I ++); do echo $ (expr $ I \ * 4); done
2. Commonly Used 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 "specifying loop content directly"
22. for I in f1 f2 f3; do
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

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 (())
#! /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. Use the while
#! /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.
For I in 'seq 1 1000000 '; do
Echo $ I
Done
I used seq 110000000 for increment. I didn't encounter any problems when I used this method, because I didn't use a million (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.
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.
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:
#! /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
}
Functiontest_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:
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.