zoukankan      html  css  js  c++  java
  • SHELL脚本编程循环篇-until循环

                 SHELL脚本编程循环篇-until循环

                                              作者:尹正杰

    版权声明:原创作品,谢绝转载!否则将追究法律责任。

    一.until循环的语法格式

    until CONDITION; do
      循环体
    done
    
    以上参数关键点说明:   CONDITION:     循环控制条件;进入循环之前,先做一次判断;每一次循环之后会再次做判断;条件为“
    true”,则执行一次循环;直到条件测试状态为“false”终止循环   进入条件:     CONDITION 为false   退出条件:     CONDITION 为true

    温馨提示:
      until循环,和while循环相反,until循环时,只要条件判断式不成立则进行循环,并执行循环程序。一旦循环条件成立,则终止循环。

    .小试牛刀

    1>.计算1-100之间的整数和

    [root@node101.yinzhengjie.org.cn ~]# cat shell/sum.sh 
    #!/bin/bash
    #
    #********************************************************************
    #Author:        yinzhengjie
    #QQ:             1053419035
    #Date:             2019-11-25
    #FileName:        shell/sum.sh
    #URL:             http://www.cnblogs.com/yinzhengjie
    #Description:        The test script
    #Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
    #********************************************************************
    
    sum=0
    read -t 30 -p "Please enter the start number>>> " StartNumber
    read -t 30 -p "Please enter an end number>>> " EndNumber
    
    until [ $StartNumber -gt $EndNumber ]
        do
            sum=$(( $sum + $StartNumber ))
            StartNumber=$(( $StartNumber + 1 ))
    done
    
    StartNumber=$(( $StartNumber - $EndNumber ))
    
    echo "从$StartNumber加到$EndNumber的总和是:$sum"
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# bash shell/sum.sh 
    Please enter the start number>>> 1
    Please enter an end number>>> 100
    从1加到100的总和是:5050
    [root@node101.yinzhengjie.org.cn ~]# 
    参考案例

    2>.打印100以内的奇数

    [root@node101.yinzhengjie.org.cn ~]# cat shell/continue.sh
    #!/bin/bash
    #
    #********************************************************************
    #Author:        yinzhengjie
    #QQ:             1053419035
    #Date:             2019-11-25
    #FileName:        shell/continue.sh
    #URL:             http://www.cnblogs.com/yinzhengjie
    #Description:        The test script
    #Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
    #********************************************************************
    
    i=0
    until [ "$i" -gt 99 ];do
        let i++
        if [ "$[i%2]" -eq 0 ];then
           continue    
        fi
        echo i=$i
    done
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# bash shell/continue.sh
    i=1
    i=3
    i=5
    i=7
    i=9
    i=11
    i=13
    i=15
    i=17
    i=19
    i=21
    i=23
    i=25
    i=27
    i=29
    i=31
    i=33
    i=35
    i=37
    i=39
    i=41
    i=43
    i=45
    i=47
    i=49
    i=51
    i=53
    i=55
    i=57
    i=59
    i=61
    i=63
    i=65
    i=67
    i=69
    i=71
    i=73
    i=75
    i=77
    i=79
    i=81
    i=83
    i=85
    i=87
    i=89
    i=91
    i=93
    i=95
    i=97
    i=99
    [root@node101.yinzhengjie.org.cn ~]# 
    参考案例1
    [root@node101.yinzhengjie.org.cn ~]# cat shell/continue.sh
    #!/bin/bash
    #
    #********************************************************************
    #Author:        yinzhengjie
    #QQ:             1053419035
    #Date:             2019-11-25
    #FileName:        shell/continue.sh
    #URL:             http://www.cnblogs.com/yinzhengjie
    #Description:        The test script
    #Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
    #********************************************************************
    
    for((i=1;i<=100;i++));do
        if [ $[i%2] -eq 0 ];then
            continue
        fi
        echo i="$i"
    done
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# bash shell/continue.sh
    i=1
    i=3
    i=5
    i=7
    i=9
    i=11
    i=13
    i=15
    i=17
    i=19
    i=21
    i=23
    i=25
    i=27
    i=29
    i=31
    i=33
    i=35
    i=37
    i=39
    i=41
    i=43
    i=45
    i=47
    i=49
    i=51
    i=53
    i=55
    i=57
    i=59
    i=61
    i=63
    i=65
    i=67
    i=69
    i=71
    i=73
    i=75
    i=77
    i=79
    i=81
    i=83
    i=85
    i=87
    i=89
    i=91
    i=93
    i=95
    i=97
    i=99
    [root@node101.yinzhengjie.org.cn ~]# 
    参考案例2

    3>.编写监控脚本,若服务出现异常则重新启动该服务

    [root@node101.yinzhengjie.org.cn ~]# cat shell/check_httpd.sh
    #!/bin/bash
    #
    #********************************************************************
    #Author:        yinzhengjie
    #QQ:             1053419035
    #Date:             2019-11-25
    #FileName:        shell/check_httpd.sh
    #URL:             http://www.cnblogs.com/yinzhengjie
    #Description:        The test script
    #Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
    #********************************************************************
    
    SLEEPTIME=30
    until false;do
        if ! killall -0 httpd &> /dev/null ;then
            systemctl restart httpd &> /dev/null
        echo "At `date +'%F %T'` httpd restarted" >> /root/httpd.log
        break
        fi
        sleep $SLEEPTIME
    done
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# bash shell/check_httpd.sh
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# ss -ntl
    State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port              
    LISTEN     0      128                          *:80                                       *:*                  
    LISTEN     0      128                          *:22                                       *:*                  
    LISTEN     0      128                         :::22                                      :::*                  
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# cat httpd.log 
    At 2019-11-25 16:26:39 httpd restarted
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# date 
    Mon Nov 25 16:27:04 CST 2019
    [root@node101.yinzhengjie.org.cn ~]# 
    参考案例

    4>.随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出

    5>.每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并退出脚本

    [root@node101.yinzhengjie.org.cn ~]# cat shell/guess.sh
    #!/bin/bash
    #
    #********************************************************************
    #Author:        yinzhengjie
    #QQ:             1053419035
    #Date:             2019-11-25
    #FileName:        shell/guess.sh
    #URL:             http://www.cnblogs.com/yinzhengjie
    #Description:        The test script
    #Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
    #********************************************************************
    
    n=$[RANDOM%11]
    while read -p "input a 0-10 number: " num;do
        if [ $num -gt  $n ];then
            echo "$num is greater"
        elif [ $num -lt $n ];then
            echo "$num is lower"
        else
            echo "guess right!"
        break
        fi
    done
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# bash shell/guess.sh
    input a 0-10 number: 5
    5 is greater
    input a 0-10 number: 3
    3 is greater
    input a 0-10 number: 1
    guess right!
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# 
    参考案例

    6>.用文件名做为参数,统计所有参数文件的总行数

    7>.用二个以上的数字为参数,显示其中的最大值和最小值

    8>.将for循环和while循环的案例使用until循环改写

    博主推荐阅读:
      (while循环)https://www.cnblogs.com/yinzhengjie/p/9158207.html
      (for循环)https://www.cnblogs.com/yinzhengjie/p/7684782.html
  • 相关阅读:
    51 数据中重复的数字
    64 数据流中的中位数
    79. Word Search
    93. Restore IP Addresses
    547. Friend Circles
    Epplus Excel 导入 MSSQL 数据库
    用来更新服务的bat 脚本
    ASP.Net MVC 引用动态 js 脚本
    8、结构的构造器应该显式调用!!!(坑)
    Task 线程重用导致等待!
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/9220743.html
Copyright © 2011-2022 走看看