语法:
for 变量名 in 变量取值列表
do
指令…
done
提示:在此结构中“in 变量取值列表”可省略,省略时相当于in “$@”,使用for i 就相当于for i in “$@”。
语法:
for((exp1;exp2;exp3))
do
指令…
Done
例:while循环和for循环的对比
[root@shellbiancheng jiaobenlianxi]# cat while4.sh
#!/bin/bash
i=1
while((i<=10))
do
echo $i
((i++))
done
[root@shellbiancheng jiaobenlianxi]# cat for1.sh
#!/bin/bash
for((i=1;i<=10;i++))
do
echo $i
done
说明:
(1)程序持续运行多用while,包括守护进程还有配合read读入循环处理。
(2)有限次循环多用for,工作中for使用更多。
学习记忆的方法:
for 男人 in 世界
do
if [ 有房 ] && [ 有车 ] && [ 存款 ] && [ 会做家务 ] && [ 帅气 ] && [ 体贴 ] && [ 逛街买东西 ];then
echo “我喜欢你”
else
rm –f 男人
fi
done
方法1:
[root@shellbiancheng jiaobenlianxi]# cat for3.sh
for n in 1 2 3 4 5
do
echo $n
done
方法2:使用大括号的方法
[root@shellbiancheng jiaobenlianxi]# cat for4.sh
for n in {1..10}
do
echo 192.168.1.$n
done
方法3:使用seq
[root@shellbiancheng jiaobenlianxi]# cat for5.sh
for n in `seq 5`
do
echo 192.168.1.$n
done
用for循环打印当前目录的文件名,用seq实现
提示:变量取值列表也可以跟命令的输出
[root@shellbiancheng jiaobenlianxi]# cat for5.sh
for n in `ls`
do
echo $n
done
批量生成文件,文件名随机md5处理后,选八位。
[root@shellbiancheng jiaobenlianxi]# cat suijiwnjian.sh
for((i=1;i<=10;i++))
do
mkdir -p ./test
touch ./test/` echo $RANDOM|md5sum|cut -c 1-8`_finished.html
done
[root@shellbiancheng jiaobenlianxi]# ls test/
0ac2453a_finished.html 8b90c52b_finished.html c4eb22c9_finished.html
2c1f5e87_finished.html a22717d3_finished.html db258218_finished.html
6d6bc69f_finished.html a4aca3b0_finished.html
6d7c516a_finished.html c21f3d55_finished.html
将上面生成的10以.html结尾的文件修改成以.jpg结尾。并且把finished去掉,只用for循环。
方法一:
[root@shellbiancheng jiaobenlianxi]# cat piliangxiugai1.sh
for f in `ls test/*.html`
do
mv $f `echo $f|sed ‘s#_finished.html#.jpg#g‘`
done
[root@shellbiancheng jiaobenlianxi]# ls test/
0ac2453a.jpg 6d6bc69f.jpg 8b90c52b.jpg a4aca3b0.jpg c4eb22c9.jpg
2c1f5e87.jpg 6d7c516a.jpg a22717d3.jpg c21f3d55.jpg db258218.jpg
方法二:for循环加变量部分截取的方法
[root@shellbiancheng jiaobenlianxi]# cat piliangxiugai3.sh
for file in `ls test/*.html`
do
/bin/mv $file "${file%_finished*}.jpg"
done
方法三:ls结合awk实现
[root@shellbiancheng test]# ls
0513d6f0_finished.html 6ab3573c_finished.html 8abfa660_finished.html
1a9335f3_finished.html 70018e1c_finished.html bb6763ab_finished.html
559d16bc_finished.html 74e206b5_finished.html
58fc15d7_finished.html 7878f84a_finished.html
[root@shellbiancheng test]# ls |awk -F ‘[_]‘ ‘{print "mv " $0,$1".jpg"}‘|bash
[root@shellbiancheng test]# ls
0513d6f0.jpg 559d16bc.jpg 6ab3573c.jpg 74e206b5.jpg 8abfa660.jpg
1a9335f3.jpg 58fc15d7.jpg 70018e1c.jpg 7878f84a.jpg bb6763ab.jpg
方法四:通过rename实现
[root@shellbiancheng test]# ls
16d72ca3_finished.html 673c62da_finished.html b9328787_finished.html
37690b31_finished.html 72ebc17d_finished.html e37aeed5_finished.html
37e66161_finished.html a5050e54_finished.html
4efe1f4c_finished.html b2553039_finished.html
[root@shellbiancheng test]# rename "_finished.html" ".jpg" *
[root@shellbiancheng test]# ls
16d72ca3.jpg 37e66161.jpg 673c62da.jpg a5050e54.jpg b9328787.jpg
37690b31.jpg 4efe1f4c.jpg 72ebc17d.jpg b2553039.jpg e37aeed5.jpg
实战案例:开发脚本实现仅设置sshd crond rsyslog network 开机自启动
[root@shellbiancheng jiaobenlianxi]# cat chkconfigoff.sh
LANG=en
chkconfig --list|grep 3:on|awk ‘{print $1}‘ >b.log
for name in `chkconfig --list|grep 3:on|awk ‘{print $1}‘|egrep -v "sshd|crond|rsyslog|network"`
do
chkconfig $name off
done
[root@shellbiancheng jiaobenlianxi]# cat chkconfigon.sh
LANG=en
for name in `cat b.log`
do
chkconfig $name on
done
方法一:
[root@shellbiancheng jiaobenlianxi]# cat for1-100.sh
for((i=1;i<=100;i++))
do
let sum+=i
done
echo $sum
[root@shellbiancheng jiaobenlianxi]# sh for1-100.sh
5050
方法二:
[root@shellbiancheng jiaobenlianxi]# cat for1-1000.sh
for n in `seq 100`
do
((sum+=n))
done
echo $sum
方法三:
[root@shellbiancheng jiaobenlianxi]# cat for1-100_3.sh
for n in `seq 100`
do
a="$(($n*($n+1)/2))"
done
echo $a
原文地址:http://blog.51cto.com/10642812/2103385