鸟哥的Linux私房菜读书笔记--循环loop

版权声明:本文属博主原创,转载请联系QQ528055624 https://blog.csdn.net/qq_41825534/article/details/83017571

1、while do done ,until do done(不定循环)

        while  [  condition  ]         #中间括号内的状态就是判断式

       do                                    #循环的开始

                        程序段落

      done                                 #循环的结束

当condition条件成立时,就终止循环,否则就持续进行循环的程序段

[hadoop1@hadoop1 temp]$ sh yes_to_stop.sh un
please input yes/YES to stop this program;un
please input yes/YES to stop this program;yes
ok you input right
#!/bin/bash
#program
#       repeat question until user input correct answer

#history
#       20181011        lile    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:/usr/local/sbin:~/temp
export PATH

while [ "${yn}" != "yes"  -a  "${yn}" != "YES" ]
do
        read -p "please input yes/YES to stop this program;" yn
done
echo "ok you input right"                                              

如果将while变成until

[hadoop1@hadoop1 temp]$ sh yes_to_stop_until.sh yes
ok you input right
[hadoop1@hadoop1 temp]$ sh yes_to_stop_until.sh no
ok you input right

#!/bin/bash
#program
#       repeat question until user input correct answer

#history
#       20181011        lile    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:/usr/local/sbin:~/temp
export PATH

until [ "${yn}" != "yes"  -o  "${yn}" != "YES" ]
do
        read -p "please input yes/YES to stop this program;" yn
done
echo "ok you input right"                              

我们可以看出,不管输入什么,until都跳出循环

例:设计计算1+2+3+4+5+6……+100

#!/bin/bash
#program
#       use loop to calculate "1 to 100' result
#history
#       20181011        lile    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH

s=0                          #加总的数值变数  sum
i=0                          #累加数值
while [ ${i} != "100" ]
do
        i=$(($i+2))
        s=$(($s+$i))
done
echo "total is $s"

2、for …in…do……done (固定循环)

相对于while与until的循环方式必须要符合某个条件的状态,for语法则需要知道循环的次数,其语法结构如下:

for  var  in  con1 con2 con3……     #var变量在循环时第一次的循环内容是con1,第二次循环内容是com2,以此类推

do

        程序段

done

[hadoop1@hadoop1 temp]$ vi for_animal.sh
#!/bin/bash
#program
#       usring for ... loop to print 3 animals
#hisroty
#       20181012        lile    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export=PATH

for animal in dog cat pig xiaolei
do
        echo -e "there are ${animal} "
done
[hadoop1@hadoop1 temp]$ sh for_animal.sh
there are dog 
there are cat 
there are pig 
there are xiaolei 

由于系统上面的各种账号都是写在/etc/passwd内的第一个字段,,我们需要透过管线命令cut捉出单纯的账号名称后,以id分别检查使用者的标识符和特殊参数。代码如下

如果我想利用 ping来判断网络状态,来进行网络状态的实际侦测中,我要侦测的网域是本机所在的192.168.122.1~192.168.122

.100,指令如下:

#!/bin/bash
#program
#       use ping command to check the network's pc state
#history
#       20181011        lile    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH

network="192.168.122"           #定义网域的前一部分
for sitenu in $(seq 1 100)      #seq为sequence(连续)的缩写
do
        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

除了使用$(seq 1 100) 以外,还可以直接使用bash的内建机制{1..100}来取代连续输入的意思,中间以两个小数点来表示连续出现的意思

例:用户输入某个目录文件名,找出某目录内的文件名的权限。

#!/bin/bash
#program
#       try do calculate 1+...+$(your input)
#history
#       20181012        lile    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

read -p "please input a number ,it will count fot 1+...+" num
s=0 i=0
for((i=0;i<=${num};i++))
do
        s=$((${s}+${i}))
done
echo -e "1+...+${num}=${s}"

3、for……do……done的数值处理

for((初始值;限制值;执行步阶))

do

                 程序段

done

初始值:某个变量在循环当中的起始值,直接以类似i=1设定好

限制值:当变量的值在这个范围内,就执行循环,例如i<=100

执行步阶:每做一次循环时,变量的变化量。例如i=i+1,也可以使用i++

#!/bin/bash
#program
#       try do calculate 1+...+$(your input)
#history
#       20181012        lile    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

read -p "please input a number ,it will count fot 1+...+" num
s=0 i=0
for((i=0;i<=${num};i++))
do
        s=$((${s}+${i}))
done
echo -e "1+...+${num}=${s}"

4、搭配随机数与数组的实验

例 :假设你所在的团队,经常为吃午饭的选择问题搞昏,,我们是否可以使用脚本搭配随机数来确定午餐。要达成这个任务,首先我们需要将全部的店家输入到一组数组当中,在透过随机数的处理,去取得可能得数值,在将搭配到该数值的店家显示出来。

#!/bin/bash
#/program
#       try to chioce what us may eat
#history
#       20181012        LILE    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH

eat[0]="humburger"
eat[1]="fried chicken"
eat[2]="handy"
eat[3]="freid bread stick"
eat[4]="instant noodles"
eatnum=5                                #需要输入可选择餐厅数
check=$((${RANDOM}*${eatnum}/32767+1))
echo -e "we will eat ${eat[${check}]}"

每次显示三个选择

#!/bin/bash
#/program
#       try to chioce what us may eat
#history
#       20181012        LILE    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH

eat[0]="humburger"
eat[1]="fried chicken"
eat[2]="handy"
eat[3]="freid bread stick"
eat[4]="instant noodles"
eatnum=5
eated=0
while [ "${eated}" -lt 3 ];do
        check=$((${RANDOM}*${eatnum}/32767+1))  #产生随机选择
        mycheck=0                              
        if [ "${eated}" -ge 1 ];then            #如果eated>1,执行以下程序
                for i in $(seq 1 ${eated})      #i从1增加到eated
                do
                        if [ ${eatedcon[${i}]} == ${check} ];then  #如果已经产生了的选择 
                                                                     mycheck=1
                                mycheck=1
                        fi
                done
        fi
        if [ ${mycheck} == 0 ];then             #如果mycheck=0,显示随机选择的结果
                echo "your may eat ${eat[${check}]}"
                eated=$((${eated}+1))
                eatedcon[${eated}]=${check}     #为了避免产生相同的选择
        fi
done

猜你喜欢

转载自blog.csdn.net/qq_41825534/article/details/83017571