1. 程式人生 > >shell編程:for 循環

shell編程:for 循環

exc alt -- 通配符 nbsp for 循環 attr put 保存

hell 編程——for in 循環

-------for in 格式-------
  1. for 無$變量 in 字符串
  2. do
  3. $變量
  4. done
一簡單的字符串 枚舉遍歷法,利用for in格式對字符串按空格切份的功能
  1. SERVICES="80 22 25 110 8000 23 20 21 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--------------字符串數組依次賦值
  1. #!/bin/sh
  2. for i in a b c 字符串列表A B C
  3. 字符串用空格分隔,沒有括號,沒有逗號, 然後循環將其依次賦給變量i
  4. 變量沒有$
  5. do
  6. echo "i is $i"
  7. done
[[email protected] ~]$ sh test.sh i is a i is b i is c -------for in 裏,變量和*不等價-------
  1. #!/bin/bash
  2. for i in *.h ;
  3. do
  4. cat ${i}.h
  5. done
[[email protected] test]$ ./tip.sh cat: *.h.h: No such file or directory $i代表的是整個路徑,而不是*.h裏的.h前面的部分 改正
  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.sh hahaha ha 例2:
  1. for i in /etc/profile.d/*.sh
  2. do
  3. $i
  4. done
$i代表的是/etc/profile.d/color.sh, /etc/profile.d/alias.sh, /etc/profile.d/default.sh -------for in 對(命令行,函數)參數遍歷-------
  1. test()
  2. {
  3. local i
  4. for i in $* ; do
  5. echo "i is $i"
  6. done
  7. }
$*是字符串:以"參數1 參數2 ... " 形式保存所有參數 $i是變量i的應用表示 [[email protected] ~]$ sh test.sh p1 p2 p3 p4 i is p1 i is p2 i is p3 i is p4 ------- for in語句與通配符*合用,批量處理文件------- 批量改文件名 [[email protected] testtip]# ls aaa.txt ccc.txt eee.txt ggg.txt hhh.txt jjj.txt lll.txt nnn.txt bbb.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相當於一個字符串數組,依次循環賦值給i
  2. do
  3. mv "$i" "$i.bak"
  4. done
[[email protected] testtip]# sh go.sh [[email protected] testtip]# ls aaa.txt.bak ccc.txt.bak eee.txt.bak ggg.txt.bak hhh.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 in語句與` `和$( )合用,利用` `或$( )的將多行合為一行的缺陷,實際是合為一個字符串數組-------
  1. for i in $(ls *.txt)
  2. do
  3. echo $i
  4. done
[[email protected] ~]$ sh test 111-tmp.txt 111.txt 22.txt 33.txt 或者說,利用for in克服` `和$( ) 的多行合為一行的缺陷 -------利用for in 自動對字符串按空格遍歷的特性,對多個目錄遍歷-------
  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循環寫法總結********
  1. for((i=1;i<=10;i++));do echo $(expr $i \* 4);done
  2. 在shell中常用的是 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)
for in語句與` `和$( )合用,利用` `或$( )的將多行合為一行的缺陷,實際是合為一個字符串數組 ============ -_- ==============for num in $(seq 1 100)
  1. LIST="rootfs usr data data2"
  2. for d in $LIST; do
  3. 用for in語句自動對字符串按空格遍歷的特性,對多個目錄遍歷
  4. for i in {1..10}
  5. for i in stringchar {1..10}
  6. awk ‘BEGIN{for(i=1; i<=10; i++) print i}‘
註意:AWK中的for循環寫法和C語言一樣的 ===============================================================
  1. #/bin/bash
  2. # author: 周海漢
  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 "參數,\$*表示腳本輸入的所有參數:"
  13. for i in $* ; do
  14. echo $i
  15. done
  16. echo
  17. echo ‘處理文件 /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 "直接指定循環內容"
  22. for i in f1 f2 f3 ;do
  23. echo $i
  24. done
  25. echo
  26. echo "C 語法for 循環:"
  27. for (( i=0; i<10; i++)); do
  28. echo $i
  29. done
--------------------------------------------------------------------------------------------------------- shell中for循環用法 shell語法好麻煩的,一個循環都弄了一會 ,找了幾個不同的方法來實現輸出1-100間可以被3整除的數 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.使用`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.使用while
  1. #!/bin/bash
  2. clear
  3. i=1
  4. while(($i<100))
  5. do
  6. if(($i%3==0))
  7. then
  8. echo $i
  9. fi
  10. i=$(($i+1))
  11. done
-------------------------------------------------------------------------------------------------------- 在shell用for循環做數字遞增的時候發現問題,特列出shell下for循環的幾種方法: 1.
  1. for i in `seq 1 1000000`;do
  2. echo $i
  3. done
用seq 1 10000000做遞增,之前用這種方法的時候沒遇到問題,因為之前的i根本就沒用到百萬(1000000),因為項目需要我這個數字遠大於百萬,發現用seq 數值到 1000000時轉換為1e+06,根本無法作為數字進行其他運算,或者將$i有效、正確的取用,遂求其他方法解決,如下 2.
  1. for((i=1;i<10000000;i++));do
  2. echo $i
  3. done
3.
  1. i=1
  2. while(($i<10000000));do
  3. echo $i
  4. i=`expr $i + 1`
  5. done
因為本方法調用expr故運行速度會比第1,第2種慢不少不過可稍作改進,將i=`expr $i + 1`改為i=$(($i+1))即可稍作速度的提升,不過具體得看相應shell環境是否支持 4.
  1. for i in {1..10000000;do
  2. echo $i
  3. done
其實選用哪種方法具體還是得由相應的shell環境的支持,達到預期的效果,再考慮速度方面的問題。 [[email protected] mnt]# ll -rw-r--r-- 1 root root 0 Mar 28 14:24 test.20130326 -rw-r--r-- 1 root root 0 Mar 28 14:24 test.20130327 -rw-r--r-- 1 root root 0 Mar 28 14:24 test.20130328 -rw-r--r-- 1 root root 0 Mar 28 14:24 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.20130328 done

shell編程:for 循環