標籤:nbsp tab 變數 roo while 列表 one exp 遞增
hell 編程——for in 迴圈
-------for in 格式-------
- for 無$變數 in 字串
- do
- $變數
- done
一簡單的字串 枚舉遍曆法,利用for in格式對字串按空格切份的功能
- SERVICES="80 22 25 110 8000 23 20 21 3306 "
-
- for x in $SERVICES
- do
- iptables -A INPUT -p tcp --dport $x -m state --state NEW -j ACCEPT
- done
-------for variable in values--------------字串數組依次賦值
- #!/bin/sh
- for i in a b c 字串列表A B C
- 字串用空格分隔,沒有括弧,沒有逗號, 然後迴圈將其依次賦給變數i
- 變數沒有$
- do
- echo "i is $i"
- done
[[email protected] ~]$ sh test.shi is ai is bi is c -------for in 裡,變數和*不等價-------
- #!/bin/bash
- for i in *.h ;
- do
- cat ${i}.h
- done
[[email protected] test]$ ./tip.shcat: *.h.h: No such file or directory $i代表的是整個路徑,而不是*.h裡的.h前面的部分改正
- #!/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 例2:
- for i in /etc/profile.d/*.sh
- do
- $i
- done
$i代表的是/etc/profile.d/color.sh,/etc/profile.d/alias.sh, /etc/profile.d/default.sh -------for in 對(命令列,函數)參數遍曆-------
- test()
- {
- local i
- for i in $* ; do
- echo "i is $i"
- done
- }
$*是字串:以"參數1 參數2 ... " 形式儲存所有參數 $i是變數i的應用表示[[email protected] ~]$ sh test.sh p1 p2 p3 p4 i is p1i is p2i is p3i is p4 ------- for in語句與萬用字元*合用,批量處理檔案------- 批量改檔案名稱[[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相當於一個字串數組,依次迴圈賦值給i
- 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 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語句與` `和$( )合用,利用` `或$( )的將多行合為一行的缺陷,實際是合為一個字串數組-------
- for i in $(ls *.txt)
- do
- echo $i
- done
[[email protected] ~]$ sh test111-tmp.txt111.txt22.txt33.txt或者說,利用for in克服` `和$( ) 的多行合為一行的缺陷 -------利用for in 自動對字串按空格遍曆的特性,對多個目錄遍曆-------
- 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迴圈寫法總結********
- for((i=1;i<=10;i++));do echo $(expr $i \* 4);done
- 在shell中常用的是 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 ;do
- for i in *.txt
- for i in $(ls *.txt)
for in語句與` `和$( )合用,利用` `或$( )的將多行合為一行的缺陷,實際是合為一個字串數組 ============ -_- ==============for num in $(seq 1 100)
- LIST="rootfs usr data data2"
- for d in $LIST; do
- 用for in語句自動對字串按空格遍曆的特性,對多個目錄遍曆
- for i in {1..10}
- for i in stringchar {1..10}
- awk ‘BEGIN{for(i=1; i<=10; i++) print i}‘
注意:AWK中的for迴圈寫法和C語言一樣的===============================================================
- #/bin/bash
- # author: 周海漢
- # 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 "參數,\$*表示指令碼輸入的所有參數:"
- for i in $* ; do
- echo $i
- done
- echo
- echo ‘處理檔案 /proc/sys/net/ipv4/conf/*/accept_redirects:‘
- for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
- echo $File
- done
- echo "直接指定迴圈內容"
- for i in f1 f2 f3 ;do
- echo $i
- done
- echo
- echo "C 文法for 迴圈:"
- for (( i=0; i<10; i++)); do
- echo $i
- done
--------------------------------------------------------------------------------------------------------- shell中for迴圈用法shell文法好麻煩的,一個迴圈都弄了一會 ,找了幾個不同的方法來實現輸出1-100間可以被3整除的數1.用(())
- #!/bin/bash
- clear
- for((i=1;i<100;i++))
- for
- do
- if((i%3==0))
- then
- echo $i
- continue
- fi
- done
2.使用`seq 100`
- #!/bin/bash
- clear
-
- for i in `seq 100`
- do
- if((i%3==0))
- then
- echo $i
- continue
- fi
- done
3.使用while
- #!/bin/bash
- clear
-
- i=1
- while(($i<100))
- do
- if(($i%3==0))
- then
- echo $i
- fi
- i=$(($i+1))
- done
--------------------------------------------------------------------------------------------------------在shell用for迴圈做數字遞增的時候發現問題,特列出shell下for迴圈的幾種方法: 1.
- for i in `seq 1 1000000`;do
-
- echo $i
-
- done
用seq 1 10000000做遞增,之前用這種方法的時候沒遇到問題,因為之前的i根本就沒用到百萬(1000000),因為項目需要我這個數字遠大於百萬,發現用seq 數值到 1000000時轉換為1e+06,根本無法作為數字進行其他運算,或者將$i有效、正確的取用,遂求其他方法解決,如下 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
因為本方法調用expr故運行速度會比第1,第2種慢不少不過可稍作改進,將i=`expr $i + 1`改為i=$(($i+1))即可稍作速度的提升,不過具體得看相應shell環境是否支援 4.
- for i in {1..10000000;do
-
- echo $i
-
- 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
- #!/bin/bash
- D=`date +%Y%m%d`
- for A in `ls | grep $D`
- do
- echo "$A"
[[email protected] mnt]# ./aa.sh test.20130328done
shell編程:for 迴圈