欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

3.5 循环(loop)

程序员文章站 2022-06-25 10:10:01
...

3.5 循环(loop)

一看见循环,就想起了while和for…

3.5.1 while do done ,until do done(不定循环)

对于不定循环,最常见的便是下面两种使用方式:
当condition成立时才循环

while [ condition ]
do			# 开始循环
		程序
done		# 结束循环

当condition成立时就停止循环

until [ condition ]
do 
		程序
done

下面两段程序,都是当用户输入yse或YES时,才会停止,否则会一直提示输入:

# 使用while do
[aaa@qq.com ~]$ cd bin
[aaa@qq.com bin]$ vim yes_to_stop.sh
#!/bin/bash
# Peogram:
#       Repcat question until user input correct ansewe.
#History:
#2019/10/13     haoqiqi         First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

while [ "${yn}" != "yse" -a "${yn}" != "YES" ]
do
        read -p "Please input yes/YES to stop this program: " yn
done
echo "You have quit this program."                                    
[aaa@qq.com bin]$ cp yes_to_stop.sh yes_to_shop_2.sh
[aaa@qq.com bin]$ vim yes_to_shop_2.sh
#!/bin/bash
# Peogram:
#       Repcat question until user input correct ansewe.
#History:
#2019/10/13     haoqiqi         First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

until [ "${yn}" == "yse" -a "${yn}" == "YES" ]
do
        read -p "Please input yes/YES to stop this program: " yn
done
echo "You have quit this program."

3.5.2 for…do…done(固定循环)

因为对于for循环,一般我们都知道它会循环几次,因此叫固定循环。

for var in con1 con2 con3 ...
do 
		程序
done
# 第i次循环时,var的值为coni

举例如下:
由于系统上的各种账号都是写在/etc/passwd内的第一个字段,因此想要使用cat捕捉账号,然后用id分别检查使用者的标识符与特殊参数。

[aaa@qq.com bin]$ vim userid.sh

#!/bin/bash
# Peogram:
#       Use id finger commend to cleck system account's information.
#History:
#2019/10/13     haoqiqi         First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

users=$(cut -d ':' -fl/etc/passwd)
for username in {users}
do 
        id ${username}
done

3.5.3 for…do…done的数值处理

除了上述的方法之外,for循环还有另一种用法,即常用的用法:

for (( 初始值;限制值;执行步长))
do
	程序
done

3.5.4 搭配随机数与数组的实验

学到现在的话,其实已经将loop学的差不多了,然后正好书上有这样一个小例子,拿来和大家娱乐一样。

[aaa@qq.com bin]$ vim what_to_eat.sh
#!/bin/bash
# Peogram:
#       Try do tell you what you may eat.
#History:
#2019/10/13     haoqiqi         First release
PATH=/bin:/sbin:/usr//bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
eat[1]="肯德基"
eat[2]="麦当劳"
eat[3]="德客士"
eat[4]="星巴克"
eat[5]="红烧牛肉面"
eat[6]="黄焖鸡米饭"
eat[7]="盖浇饭"
eatnum=7

eated=0
while [ "${eated}" -lt 3 ]; do
        check=$(( ${RANDOM}*${eatnum}/32767+1))
        mycheck=0
        if [ "${eated}" -ge 1 ]; then
                for i in $(seq 1 ${eated} )
                do
                        if [ ${eatedcon[$i]} == $check ]; then
                                mycheck=1
                        fi
                done
        fi
        if [ ${mycheck} == 0 ]; then
                echo "You may eat ${eat[${check}]}"
                eated=$(( ${eated} + 1 ))
                eatedcon[${eated}]=${check}
        fi
done
[aaa@qq.com bin]$ sh what_to_eat.sh
You may eat 德客士
You may eat 星巴克
You may eat 肯德基

本节内容就这些了, 欢迎关注我的微信公众号与我联系。
3.5 循环(loop)