Hell Programming--for in loop
-------in format-------
- For no $ variable in string
- Do
- $ variable
- Done
A simple string to enumerate through the calendar, with the function of using the in format to cut the string by a space
- services="8000 3306"
- For x in $SERVICES
- Do
- Iptables-a input-p TCP--dport $x-M state--state New-j ACCEPT
- Done
-------for variable in values--------------string array to assign values sequentially
- #!/bin/sh
- For i in a B C string list A B C
- The string is separated by a space, without parentheses, without commas, and then looping to assign it to the variable i
- Variable does not have $
- Do
- echo "I is $i"
- Done
[[Email protected] ~]$ sh Test.shi is an AI is bi is a C-------for in, variables and * are not equivalent-------
- #!/bin/bash
- For i in *.h;
- Do
- Cat ${i}.h
- 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
- #!/bin/bash
- For I in *.h
- Do
- Cat $i
- Done
[Email protected] test]$ echo hahaha >>1.h[[email protected] test]$ echo ha >>2.h [[email protected] test]$. /tip.shhahahaha Example 2:
- For I in/etc/profile.d/*.sh
- Do
- $i
- 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----- --
- test ()
- {
- local i
- for i in $* ; do
- echo "i is $i"  
- done
- } 
$* 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
- For I in *.txt *.txt is equivalent to an array of strings, looping to I in turn
- Do
- MV "$i" "$i. Bak"
- 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-------
- For I in $ (LS *.txt)
- Do
- Echo $i
- 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-------
- list="rootfs usr data data2"
- For d in $LIST; Do
- mount/backup/$d
- Rsync-ax--exclude fstab--delete/$d//backup/$d/
- umount/backup/$d
- Done
Linux Shell for loop notation summary ********
- For ((i=1;i<=10;i++));d o echo $ (expr $i \* 4);d One
- Common in shell is for I in $ (seq 10)
- For i in ' ls '
- For i in ${arr[@]}
- For i in $*; Do
- For File in/proc/sys/net/ipv4/conf/*/accept_redirects; Do
- For I in F1 F2 F3;d o
- For I in *.txt
- 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)
- list="rootfs usr data data2"
- For d in $LIST; Do
- Use the for in statement to automatically traverse the character string by space, traversing multiple directories
- For i in {1..10}
- For I in Stringchar {1..10}
- awk ' Begin{for (i=1; I<=10; i++) Print i} '
Note: The For loop notation in awk is the same as the C language ===============================================================
- #/bin/bash
- # Author: Zhou Haihan
- # date:2010.3.25
- # Blog.csdn.net/ablo_zhou
- Arr= ("A" "B" "C")
- echo "Arr is (${arr[@]})"
- echo "Item in array:"
- For i in ${arr[@]}
- Do
- echo "$i"
- Done
- echo "parameter, \$* represents all parameters entered by the script:"
- For i in $*; Do
- Echo $i
- Done
- Echo
- echo ' Processing file/proc/sys/net/ipv4/conf/*/accept_redirects: '
- For File in/proc/sys/net/ipv4/conf/*/accept_redirects; Do
- Echo $File
- Done
- echo "Specify looping content directly"
- For I in F1 F2 F3;d o
- Echo $i
- Done
- Echo
- echo "C syntax for loop:"
- For (( i=0; I<i++);
- Echo $i
- 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. (())
- #!/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. Using the While
- #!/bin/bash
- Clear
- i=1
- while (($i<))
- Do
- if (($i%3==0))
- Then
- Echo $i
- Fi
- i=$ (($i + 1))
- Done
--------------------------------------------------------------------------------------------------------the shell uses the for loop to increment the number. , there are several ways to list the shell for loop: 1.
- For i in ' seq 1 1000000 ';d o
- Echo $i
- 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.
- 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 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.
- For I in {1..10000000;do
- Echo $i
- 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
- #!/bin/bash
- d= ' Date +%y%m%d '
- For A in ' ls | grep $D '
- Do
- echo "$A"
[Email protected] mnt]#/aa.sh Test.20130328done
Shell programming: For Loop