shell 編程——for in 循環shell
- for 無$變量 in 字符串
- do
- $變量
- done
- 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
- #!/bin/sh
- for i in a b c 字符串列表A B C
- 字符串用空格分隔,沒有括號,沒有逗號, 而後循環將其依次賦給變量i
- 變量沒有$
- do
- echo "i is $i"
- done
- #!/bin/bash
- for i in *.h ;
- do
- cat ${i}.h
- done
- #!/bin/bash
- for i in *.h
- do
- cat $i
- done
- for i in /etc/profile.d/*.sh
- do
- $i
- done
- test()
- {
- local i
- for i in $* ; do
- echo "i is $i"
- done
- }
- for i in *.txt *.txt至關於一個字符串數組,依次循環賦值給i
- do
- mv "$i" "$i.bak"
- done
- for i in $(ls *.txt)
- do
- echo $i
- done
- 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
- 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)
- 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}'
- #/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
- #!/bin/bash
- clear
- for((i=1;i<100;i++))
- for
- do
- if((i%3==0))
- then
- echo $i
- continue
- fi
- done
- #!/bin/bash
- clear
- for i in `seq 100`
- do
- if((i%3==0))
- then
- echo $i
- continue
- fi
- done
- #!/bin/bash
- clear
- i=1
- while(($i<100))
- do
- if(($i%3==0))
- then
- echo $i
- fi
- i=$(($i+1))
- done
- for i in `seq 1 1000000`;do
- echo $i
- done
- for((i=1;i<10000000;i++));do
- echo $i
- done
- i=1
- while(($i<10000000));do
- echo $i
- i=`expr $i + 1`
- done
- for i in {1..10000000;do
- echo $i
- done
- #!/bin/bash
- D=`date +%Y%m%d`
- for A in `ls | grep $D`
- do
- echo "$A"