您的位置:首页 > 运维架构 > Shell

shell ——for in 循环

2013-11-21 11:25 99 查看
shell ——for in 循环

2013-03-28 18:06:29

标签:for 循环

shell 编程——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

[macg@machome ~]$ sh test.sh
i is a
i is b
i is c


-------for in 里,变量和*不等价-------
#!/bin/bash 
for i in *.h ; 
do 
cat ${i}.h 
done

[macg@vm test]$ ./tip.sh
cat: *.h.h: No such file or directory
$i代表的是整个路径,而不是*.h里的.h前面的部分
改正

#!/bin/bash 
for i in *.h 
do 
cat $i 
done

[macg@vm test]$ echo hahaha >>1.h
[macg@vm test]$ echo ha >>2.h

[macg@vm test]$ ./tip.sh
hahaha
ha
例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的应用表示
[macg@machome ~]$ sh test.sh p1 p2 p3 p4

i is p1
i is p2
i is p3
i is p4


------- for in语句与通配符*合用,批量处理文件-------
批量改文件名
[root@vm 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
[root@vm testtip]# cat go.sh

for i in *.txt                 *.txt相当于一个字符串数组,依次循环赋值给i 
do 
mv "$i" "$i.bak"        
done

[root@vm testtip]# sh go.sh

[root@vm 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语句与` `和$( )合用,利用` `或$( )的将多行合为一行的缺陷,实际是合为一个字符串数组-------

for i in $(ls *.txt)         
do 
echo $i 
done

[macg@machome ~]$ sh test
111-tmp.txt
111.txt
22.txt
33.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环境的支持,达到预期的效果,再考虑速度方面的问题。


[root@mail 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"

[root@mail mnt]# ./aa.sh
test.20130328
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: