Shell script的循环有以下几种:
一、不定次数循环:while do done, until do done
1、while do done的语法
while [ condition ] #中括号内是判断条件
do #do 是循环语句开始关键字
程序代码
done #done 是循环结束关键字
该循环的含义:当 condition 条件成立时,就进行循环,直到 condition 的条件不成立进行停止
示例:
#!/bin/bash
#Program:
# Repeat question until user input correct answer.
#History:2017/01/11 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
while [ "${yn}" != "yes" -a "${yn}" != "YES" ]
do
read -p "Please input correct answer:" yn
done
echo "OK!you input the correct answer."
运行结果:
root@Test:~/bin$ sh while.sh
Please input correct answer:s
Please input correct answer:yes
OK!you input the correct answer.
2、until do done的语法
until [ condition ]
do
程序代码
done
该循环与while do done相反,当 condition 条件成立时,就停止循环, 否则一直执行循环中的程序代码。
示例:
#!/bin/bash
#Program:
# Repeat question until user input correct answer.
#History:2017/01/11 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
until [ "${yn}" == "yes" -o "${yn}" == "YES" ]
do
read -p "Please input correct answer:" yn
done
echo "OK!you input the correct answer."
运行结果:
root@Test:~/bin$ sh until.sh
Please input correct answer:s
Please input correct answer:yes
OK!you input the correct answer.
3、使用while do done实现计算1+2+3+...+100和的程序:
#!/bin/bash
#Program:
# Use loop to calculate "1+2+3+...+100" result
#History:2017/01/11 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
s=0 #设置变量
i=0
while [ "${i}" != "100" ]
do
i=$(($i+1)) #每次循环i增加1
s=$(($s+$i)) #每次都会累加
done
echo "The result of "1+2+3+...+100" is ==> $s"
运行结果:
root@Test:~/bin$ sh calsum.sh
The result of 1+2+3+...+100 is ==> 5050
1、语法:
for var in con1 con2 con3 ...
do
程序代码
done
其含义:
这个$var 的变量值在循环运行时:
第一次循环时, $var 的值为 con1 ;
第二次循环时, $var 的值为 con2 ;
第三次循环时, $var 的值为 con3 ;
示例:
#!/bin/bash
#Program:Using for...loop to print 3 animals
#History:2017/01/11 shu release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
for animal in dog cat elephant
do
echo "There are ${animal}s...."
done
运行结果:
root@Test:~/bin$ sh show_animals.sh
There are dogs....
There are cats....
There are elephants....
2、使用for...do...done 实现 ping 监测 192.168.1.1~192.168.1.100中是否与本机连通:
#!/bin/bash
#Program: Use ping command to check the network's PC state
#History: 2017/01/11 shu release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
network="192.168.2" #先定义一个网址的前面部分!
for sitenu in $(seq 1 100) #seq为sequence(连续)的缩写
do
#获取ping的回传值,成功或失败
ping -c 1 -w 1 ${network}.${sitenu} &> /dev/null && result=0 || result=1
#显示结果是正确的启动(UP)还是错误的没有连通(DOWN)
if [ "${result}" == 0 ]; then
echo "Server ${network}.${sitenu} is UP."
else
echo "Server ${network}.${sitenu} is DOWN."
fi
done
运行结果:
root@Test:~/bin$ chmod a+x pingip.sh;./pingip.sh
Server 192.168.2.1 is UP.
Server 192.168.2.2 is UP.
...
Server 192.168.2.100 is DOWN.
其中除了使用$(seq 1 100) ,也可以使用bash 的内建连续数据功能{1..100} 进行处理。
大括号里的前/后用2个字符表示,中间以2个小数点代替连续出现,例如要连续输出 a, b, c...g ,可以使用echo {a..g}
3、示例:根据用户输入的目录,查询目录下每个文件的权限
#Program:# User input dir name,I find the permission of files#History:2017/01/11 shu releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATH#1.检查输入目录是否存在read -p "Please input dir:" dirif [ "${dir}" == "" -o ! -d "${dir}" ]; then echo "The ${dir} is not exist in your system." exit 1fi#2.开始监测文件的权限filelist=$(ls ${dir}) #列出所有在该目录下的文件名称for filename in ${filelist}do perm="" test -r "${dir}/${filename}" && perm="${perm} readable" test -x "${dir}/${filename}" && perm="${perm} executable" test -w "${dir}/${filename}" && perm="${perm} writable" echo "The file ${dir}/${filename}'s permission is ${perm}"done 运行结果:#!/bin/bash
root@Test:~bin$ chmod a+x dirperm.sh;./dirperm.sh Please input dir:/home/shu/program/bin The file /home/shu/program/bin/ans_yn1.sh's permission is readable executable writable The file /home/shu/program/bin/ans_yn.sh's permission is readable executable writable ......
三、在for...do...done中的for写入初始值;限制值;变化量的循环
语法如下:
for (( 初始值; 限制值; 变化量 )) do 程序代码 done
这种结构适用于数值方式的运算,for括号里参数的含义:
1)初始值:某个变量在循环当中的起始值,类似i=1;
2)限制值:当变量的值在这个限制值范围内,继续循环,类似i<=100;
3)变化量:每次循环,变量的变化量,类似i=i+1。在变化量的设定中,若每次增加1,可使用i++方式。
示例:运行结果:#!/bin/bash #Program: # Use loop to calculate "1+2+3+...+100" result #History:2017/01/11 shu First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH s=0 #设置变量 for (( i=1;i<=100;i++ )) do s=$((${s}+${i})) #每次都会累加 done echo "The result of "1+2+3+...+100" is ==> ${s}"
root@Test:~/bin$ chmod a+x calfor.sh;./calfor.sh The result of 1+2+3+...+100 is ==> 5050
四、使用随机阵列方式使用随机数、循环、判断语句实现多个随机选择的功能:
运行结果:#!/bin/bash #Program: Try do tell you what you may choice. #History: 2017/01/12 release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH choise[1]="learn english" #写下所有想做的事情 choise[2]="yoga practice" choise[3]="read book" choise[4]="draw picture" choise[5]="learn techniques" choise[6]="Watch TV" choise[7]="cook" choise[8]="listen radio" choisenum=8 #需要输入有几个可用的选择 choised=0 while [ "${choised}" -lt 3 ]; do check=$((${RANDOM}*${choisenum} / 32767 + 1 )) mychoise=0 if [ "${choised}" -ge 1 ];then for i in $(seq 1 ${choised} ) do if [ ${choisedcon[$i]} == $check ]; then mychoise=1 fi done fi if [ ${mychoise} == 0 ];then echo "your may choise ${choise[${check}]}" choised=$(( ${choised} + 1 )) choisedcon[${choised}]=${check} fi done
root@Test:~/bin$ chmod a+x morechoise.sh;./morechoise.sh your may choise learn english your may choise draw picture your may choise cook