Summary of shellfor statement

Source: Internet
Author: User
Summary of shellfor loop writing-Linux general technology-Linux technology and application information. The following is a detailed description. 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/conf/*/accept_redirects; do
7. for I in f1 f2 f3; do
8. for I in *. txt
9. for I in $ (ls *. txt)
The for in statement is used together with ''and $ (). The combination of multiple rows into one row using'' or $ () is actually a string array.

================= For num in $ (seq 1 100)
10. LIST = "rootfs usr data data2"
For d in $ LIST; do
The for in statement is used to automatically traverse strings by spaces and traverse multiple directories.
11. for I in {1 .. 10}
12. for I in stringchar {1 .. 10}
13. awk 'in in {for (I = 1; I <= 10; I ++) print I }'

Note: The for loop write method in AWK is the same as that in C.





========================================================== ======================================





01. #/bin/bash

02. # author: Zhou haihan

03. # date: 2010.3.25

04. # blog.csdn.net/ablo_zhou

05. arr = ("a" "B" "c ")
06. echo "arr is ($ {arr [@]})"
07. echo "item in array :"
08. for I in $ {arr [@]}
09. do
10. echo "$ I"
11. done
12. echo "Parameter \ $ * indicates all parameters input by the script :"
13. for I in $ *; do
14. echo $ I
15. done
16. echo
17. echo 'process file/proc/sys/net/ipv4/conf/*/accept_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.

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.