shell脚本编程学习笔记-for循环
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本编程学习笔记-for循环相关的知识,希望对你有一定的参考价值。
1.for循环结构
1.1 for循环结构语法
语法:
for 变量名 in 变量取值列表
do
指令…
done
提示:在此结构中“in 变量取值列表”可省略,省略时相当于in “[email protected]”,使用for i 就相当于for i in “[email protected]”。
1.2 C语言型for循环结构
语法:
for((exp1;exp2;exp3))
do
指令…
Done
例:while循环和for循环的对比
[[email protected] jiaobenlianxi]# cat while4.sh
#!/bin/bash
i=1
while((i<=10))
do
echo $i
((i++))
done
[[email protected] 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.3 for循环基础例子
1.3.1 范例1:直接列出变量列表所有元素,打印1,2,3,4,5
方法1:
[[email protected] jiaobenlianxi]# cat for3.sh
for n in 1 2 3 4 5
do
echo $n
done
方法2:使用大括号的方法
[[email protected] jiaobenlianxi]# cat for4.sh
for n in {1..10}
do
echo 192.168.1.$n
done
方法3:使用seq
[[email protected] jiaobenlianxi]# cat for5.sh
for n in `seq 5`
do
echo 192.168.1.$n
done
用for循环打印当前目录的文件名,用seq实现
提示:变量取值列表也可以跟命令的输出
[[email protected] jiaobenlianxi]# cat for5.sh
for n in `ls`
do
echo $n
done
1.3.2 范例2:随机数生成文件
批量生成文件,文件名随机md5处理后,选八位。
[[email protected] 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
[[email protected] 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
1.3.3 范例3:批量修改文件名
将上面生成的10以.html结尾的文件修改成以.jpg结尾。并且把finished去掉,只用for循环。
方法一:
[[email protected] jiaobenlianxi]# cat piliangxiugai1.sh
for f in `ls test/*.html`
do
mv $f `echo $f|sed ‘s#_finished.html#.jpg#g‘`
done
[[email protected] jiaobenlianxi]# ls test/
0ac2453a.jpg 6d6bc69f.jpg 8b90c52b.jpg a4aca3b0.jpg c4eb22c9.jpg
2c1f5e87.jpg 6d7c516a.jpg a22717d3.jpg c21f3d55.jpg db258218.jpg
方法二:for循环加变量部分截取的方法
[[email protected] jiaobenlianxi]# cat piliangxiugai3.sh
for file in `ls test/*.html`
do
/bin/mv $file "${file%_finished*}.jpg"
done
方法三:ls结合awk实现
[[email protected] 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
[[email protected] test]# ls |awk -F ‘[_]‘ ‘{print "mv " $0,$1".jpg"}‘|bash
[[email protected] test]# ls
0513d6f0.jpg 559d16bc.jpg 6ab3573c.jpg 74e206b5.jpg 8abfa660.jpg
1a9335f3.jpg 58fc15d7.jpg 70018e1c.jpg 7878f84a.jpg bb6763ab.jpg
方法四:通过rename实现
[[email protected] 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
[[email protected] test]# rename "_finished.html" ".jpg" *
[[email protected] test]# ls
16d72ca3.jpg 37e66161.jpg 673c62da.jpg a5050e54.jpg b9328787.jpg
37690b31.jpg 4efe1f4c.jpg 72ebc17d.jpg b2553039.jpg e37aeed5.jpg
1.3.4 范例4:
实战案例:开发脚本实现仅设置sshd crond rsyslog network 开机自启动
[[email protected] 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
[[email protected] jiaobenlianxi]# cat chkconfigon.sh
LANG=en
for name in `cat b.log`
do
chkconfig $name on
done
1.3.5 范例5:用for循环实现1-100之和
方法一:
[[email protected] jiaobenlianxi]# cat for1-100.sh
for((i=1;i<=100;i++))
do
let sum+=i
done
echo $sum
[[email protected] jiaobenlianxi]# sh for1-100.sh
5050
方法二:
[[email protected] jiaobenlianxi]# cat for1-1000.sh
for n in `seq 100`
do
((sum+=n))
done
echo $sum
方法三:
[[email protected] jiaobenlianxi]# cat for1-100_3.sh
for n in `seq 100`
do
a="$(($n*($n+1)/2))"
done
echo $a
以上是关于shell脚本编程学习笔记-for循环的主要内容,如果未能解决你的问题,请参考以下文章