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 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.
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
}
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:
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
Summary of the For Loop in Shell