Shell programming: For Loop

Source: Internet
Author: User

Hell Programming--for in loop

-------in format-------
    1. For no $ variable in string
    2. Do
    3. $ variable
    4. Done
A simple string to enumerate through the calendar, with the function of using the in format to cut the string by a space
    1. services="8000 3306"
    2. For x in $SERVICES
    3. Do
    4. Iptables-a input-p TCP--dport $x-M state--state New-j ACCEPT
    5. Done
-------for variable in values--------------string array to assign values sequentially
    1. #!/bin/sh
    2. For i in a B C string list A B C
    3. The string is separated by a space, without parentheses, without commas, and then looping to assign it to the variable i
    4. Variable does not have $
    5. Do
    6. echo "I is $i"
    7. Done
[[Email protected] ~]$ sh Test.shi is an AI is bi is a C-------for in, variables and * are not equivalent-------
    1. #!/bin/bash
    2. For i in *.h;
    3. Do
    4. Cat ${i}.h
    5. Done
[Email protected] test]$./TIP.SHCAT: *.h.h:no such file or directory $i represents the entire path, not the *.h. h part of the previous correction
    1. #!/bin/bash
    2. For I in *.h
    3. Do
    4. Cat $i
    5. Done
[Email protected] test]$ echo hahaha >>1.h[[email protected] test]$ echo ha >>2.h [[email protected] test]$. /tip.shhahahaha Example 2:
    1. For I in/etc/profile.d/*.sh
    2. Do
    3. $i
    4. Done
$i represents/etc/profile.d/color.sh,/etc/profile.d/alias.sh,/etc/profile.d/default.sh-------for-in pair (command line, function) parameter traversal----- --
    1. test ()  
    2.          local i 
    3.         for i  in $* ; do 
    4.               echo  "i is  $i"  
    5.          done 
    6. } 
$* is a string: with "parameter 1 parameter 2 ... "The form holds all parameters $i is the application of the variable i represents [[email protected] ~]$ sh test.sh p1 p2 p3 P4 I am p1i is P2i are P3i is a P4-------for in statement with wildcard * , Batch processing file-------batch change file name [[email protected] testtip]# lsaaa.txt ccc.txt eee.txt ggg.txt hhh.txt jjj.txt lll.txt nnn. Txtbbb.txt ddd.txt fff.txt go.sh iii.txt kkk.txt mmm.txt ooo.txt[[email protected] testtip]# cat go.sh
    1. For I in *.txt *.txt is equivalent to an array of strings, looping to I in turn
    2. Do
    3. MV "$i" "$i. Bak"
    4. Done
[[Email protected] testtip]# sh go.sh [[email protected] testtip]# Lsaaa.txt.bak ccc.txt.bak eee.txt.bak ggg.txt.bak H Hh.txt.bak jjj.txt.bak lll.txt.bak nnn.txt.bak bbb.txt.bak ddd.txt.bak fff.txt.bak go.sh iii.txt.bak kkk.txt . bak Mmm.txt.bak Ooo.txt.bak-------for the in statement with "' and $ (), using the" or $ () of a line of multiple lines of defects, is actually a string array-------
    1. For I in $ (LS *.txt)
    2. Do
    3. Echo $i
    4. Done
[[Email protected] ~]$ sh test111-tmp.txt111.txt22.txt33.txt or, using for in to overcome the multiple lines of "and $ () a line of defects-------take advantage of the for-in automatic string traversal by space Features, traversal of multiple directories-------
    1. list="rootfs usr data data2"
    2. For d in $LIST; Do
    3. mount/backup/$d
    4. Rsync-ax--exclude fstab--delete/$d//backup/$d/
    5. umount/backup/$d
    6. Done
Linux Shell for loop notation summary ********
    1. For ((i=1;i<=10;i++));d o echo $ (expr $i \* 4);d One
    2. Common 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;d o
    8. For I in *.txt
    9. For I in $ (LS *.txt)
The for-in statement is used in combination with "' and $ (), and the defect that uses the" or $ () to line up a row is actually a string array ============-_-==============for num in $ (SEQ 1 100)
    1. list="rootfs usr data data2"
    2. For d in $LIST; Do
    3. Use the for in statement to automatically traverse the character string by space, traversing multiple directories
    4. For i in {1..10}
    5. For I in Stringchar {1..10}
    6. awk ' Begin{for (i=1; I<=10; i++) Print i} '
Note: The For loop notation in awk is the same as the C language ===============================================================
  1. #/bin/bash
  2. # Author: Zhou Haihan
  3. # date:2010.3.25
  4. # Blog.csdn.net/ablo_zhou
  5. Arr= ("A" "B" "C")
  6. echo "Arr is (${arr[@]})"
  7. echo "Item in array:"
  8. For i in ${arr[@]}
  9. Do
  10. echo "$i"
  11. Done
  12. echo "parameter, \$* represents all parameters entered by the script:"
  13. For i in $*; Do
  14. Echo $i
  15. Done
  16. Echo
  17. echo ' Processing 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 "Specify looping content directly"
  22. For I in F1 F2 F3;d o
  23. Echo $i
  24. Done
  25. Echo
  26. echo "C syntax for loop:"
  27. For (( i=0; I<i++);
  28. Echo $i
  29. Done
---------------------------------------------------------------------------------------------------------shell for loop usage sh ell syntax good trouble, a loop has been a while, found a few different ways to achieve output 1-100 can be divisible by 3 of the number 1. (())
    1. #!/bin/bash
    2. Clear
    3. For ((i=1;i<100;i++ )
    4. For
    5. Do
    6. if ((i%3==0))
    7. Then
    8. Echo $i
    9. Continue
    10. Fi
    11. Done
2. Use ' SEQ 100 '
    1. #!/bin/bash
    2. Clear
    3. For i in ' SEQ 100 '
    4. Do
    5. if ((i%3==0))
    6. Then
    7. Echo $i
    8. Continue
    9. Fi
    10. Done
3. Using the While
    1. #!/bin/bash
    2. Clear
    3. i=1
    4. while (($i<))
    5. Do
    6. if (($i%3==0))
    7. Then
    8. Echo $i
    9. Fi
    10. i=$ (($i + 1))
    11. Done
--------------------------------------------------------------------------------------------------------the shell uses the for loop to increment the number. , there are several ways to list the shell for loop: 1.
    1. For i in ' seq 1 1000000 ';d o
    2. Echo $i
    3. Done
With seq 1 10000000 increment, before using this method did not encounter problems, because the previous I was useless to million (1000000), because the project needs me this number is far greater than million, found that the SEQ value to 1000000 when converted to 1e+06, Can not be used as the number of other operations, or the $i valid, correct access, and then seek other methods to solve, as follows 2.
    1. For ((i=1;i<10000000;i++);d o
    2. Echo $i
    3. Done
3.
    1. i=1
    2. while ($i<10000000);d o
    3. Echo $i
    4. i= ' expr $i + 1 '
    5. Done
Because this method calls expr so the speed will be slower than the 1th, 2nd, but can be slightly improved, the i= ' expr $i + 1 ' to i=$ (($i + 1)) can be slightly increased speed, but depends on the corresponding shell environment support 4.
    1. For I in {1..10000000;do
    2. Echo $i
    3. Done
In fact, the choice of which method specific or to be supported by the corresponding shell environment, to achieve the desired results, and then consider the problem of speed. [Email protected] mnt]# ll-rw-r--r--1 root root 0 Mar 14:24 test.20130326-rw-r--r--1 root root 0 Mar 2 8 14:24 test.20130327-rw-r--r--1 root root 0 Mar 14:24 test.20130328-rw-r--r--1 root root 0 Mar 28 14:2 4 test.20130329
    1. #!/bin/bash
    2. d= ' Date +%y%m%d '
    3. For A in ' ls | grep $D '
    4. Do
    5. echo "$A"
[Email protected] mnt]#/aa.sh Test.20130328done

Shell programming: For Loop

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.