shell编程for循环总结
在shell编程中,循环的执行是将某代码段重复运行多次,常用循环有for、while和until循环,其中for循环经常用于有限次循环,for循环的语法结构有如下两种:
第一种:shell传统for循环语法结构for 变量名 in 变量取值列表;do
指令…
done
其中取值列表通常又有5种方式:
(1)直接给出列表,列表中间用空格隔开
[root@centos7 ~]#for i in 1 3 5;do echo $i;done
1
3
5
(2)整数列表
(a) {start..end}
[root@centos7 ~]#for i in {1..5..2};do echo $i;done
1
3
5
(b)$(seq start step end)
[root@centos7 ~]#for i in $(seq 1 2 5);do echo $i;done
1
3
5
(3)返回命令列表
[root@centos7 ~]#mkdir test
[root@centos7 ~]#cd test/
[root@centos7 ~/test]#touch 1.sh
[root@centos7 ~/test]#touch 3.sh
[root@centos7 ~/test]#touch 5.sh
[root@centos7 ~/test]#for i in $(ls);do echo $i;done
1.sh
3.sh
5.sh
(4)使用glob,如:*.sh
[root@centos7 ~/test]#for i in *\.sh; do echo $i;done #选择当前目录下满足*.sh
1.sh
3.sh
5.sh
(5)使用变量引用如$@,$*
[root@centos7 ~/test]#vim 1
[root@centos7 ~/test]#chmod +x 1
1 #!/bin/bash
2 for i in $@;do
3 rm -rf $i
4 done
[root@centos7 ~/test]#./1 1.sh 3.sh 5.sh
[root@centos7 ~/test]#ls
1 #验证1.sh 3.sh 5.sh已经删除
第二种结构体:C语言型结构体
for ((exp1; exp2; exp3))
do
指令
done
[root@centos7 ~/test]#for ((i=1; i<=5; i=i+2));do echo $i;done
1 #注意:a=a++ 和 a=a+2的区别,a=a+2是表达式,=号的优先级较低,因此先+后赋值
3
5
基础案例分析:
1.打印99乘方口诀,[root@centos7 ~/scripts]#vim sufakoujue.sh
#!/bin/bash
for i in $(seq 9);dofor j in $(seq $i);dolet sum=$i*$jecho -n "$j*$i=$sum "doneecho #打印换行,每循环一次进行一次换行
done
[root@centos7 ~/scripts]#chmod +x sufakoujue.sh
[root@centos7 ~/scripts]#./suanfakoujue.sh
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
2.判断当前目录下所有文件的类型[root@centos7~/test]#vim panduan.sh
#!/bin/bash
for filename in *;do #利用for语句列表中第四种方式使用通配符glob
if[ -f $filename ];then
echo $filename is file
else [ -d $filename ]
echo$filename is directory
fi
done
[root@centos7 ~/test]#chmod+x panduan.sh
[root@centos7 ~/test]#./panduan.sh
1 is file
1.sh is file
2 is file
3 is directory
3.sh is file
5 is directory
5.sh is file
panduan.sh is file
3.计算1+2+3+4+...+n之和,其中n由用户自己输入[root@centos7~/test]#vim sum.sh
#!/bin/bash
read-p "please inputyour inter: " inter
#+++++++++++++Makesure the parameter is not empty++++++++
[-z $inter ] && echo "you must input a inter!" && exit1
#+++++++++++++Makesure the parameter is integer++++++++++
if[[ $inter =~ ^[0-9]+$ ]];then
for i in $(seq $inter);do
let sum=$sum+i
done
echo"1+2+..+$i=$sum"
else
echo"you must input a inter!"
fi
[root@centos7 ~/test]#chmod+x sum.sh
[root@centos7 ~/test]#./sum.sh
please input your inter: 4
1+2+..+4=10
4.计算100之内能被3整除的整数之和[root@centos7~/test]#vim 3sum.sh
#!/bin/bash
for i in $(seq 3 3 100);do #利用整数列表步进的方式
let sum=$sum+i
done
echo "3+6+...99=$sum"
[root@centos7 ~/test]#chmod +x 3sum.sh
[root@centos7 ~/test]#./3sum.sh
3+6+...99=1683
5.判断局域网192.168.1.0的网段主机存活状态[root@centos7 ~/test]#vim hostping.sh
net=192.168.1
for i in {1..255};do
{
if ping -c1 -w1 $net.$i &>/dev/null;then
echo "$net.$i is exsit"
fi
}& #通过放入后台并行执行提高执行效率,避免顺序执行
done
wait #避免敲回车退出脚本,可以通过help wait查看帮助
[root@centos7 ~/test]#chmod +x hostping.sh
[root@centos7 ~/test]#./hostping.sh
192.168.1.48 is exsit
6.打印等腰三角形
[root@centos7 ~/test]vim dengyao.sh
read -p "please input a inter: " a
for i in $(seq$a);do
let j=$a-$i+1
let k=2*$i-1
#++++++++打印每行等腰三角的空白字符+++++
for l in $(seq $j);do
echo -n " "
done
#++++++++打印等腰三角形的构成图形+++++++
for n in $(seq $k);do
echo -n ""
done
#++++++++每一行结束后进行换行+++++++++++
echo
done
[root@centos7 ~/test]./dengyao.sh********具体详情请咨询微信:QQ767743577 邮箱地址: xuewei_bo@126.com,有问必答,有答必应,人人为我,我为人人*******