• shell脚本(10)-流程控制while与until


    一、while循环介绍

    while循环与for一样,一般不知道循环次数使用for,不知道循环的次数时推荐使用while

     

    二、while语法

    while [ condition ]  #条件为真才会循环,条件为假,while停止循环
        do
            commands
    done  

     

    三、while实战

    1、使用while遍历文件内容

    #!/usr/bin/bash
    
    while read i
      do
        echo "$i"
    done < $1

    查看运行结果:

    [root@localhost test20210728]# sh while-1.sh /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    ......

    2、while实现账户登录判断

    [root@localhost test20210728]# vim while-2.sh
    
    #!/usr/bin/bash
    
    read -p "login: " account
    while [ $account != 'root' ]
      do
        echo "输入的账户不符合条件,请重新输入:"
        read -p "login: " account
    done

    查看运行结果:

    [root@localhost test20210728]# sh while-2.sh 
    login: test
    输入的账户不符合条件,请重新输入:
    login: root1
    输入的账户不符合条件,请重新输入:
    login: root            
    [root@localhost test20210728]#

    3、while+sleep(输出1-10,且每次输出后睡眠1s)

    [root@localhost test20210728]# vim while-3+sleep.sh
    
    #!/usr/bin/bash
    i=1
    while [ $i -lt 10 ];do
      echo -n $i" "
      sleep 1
      i=$((i+1))
    done
    echo

    查询运行结果:(每秒输出一个数字)

    [root@localhost test20210728]# sh while-3+sleep.sh 
    1 2 3 4 5 6 7 8 9

    4、while+break(输出1开始自增,且到5时停止)

    [root@localhost test20210728]# vim while-4+break.sh 
    
    #!/usr/bin/bash
    i=1
    while [ $i -lt 10 ];do
      echo -n $i" "
      if [ $i -eq 5 ];then
        break
      fi
      i=$((i+1))
    done
    echo

    查看运行结果:

    [root@localhost test20210728]# sh while-4+break.sh
    1 2 3 4 5 

    5、while+continue(输出1-10开始自增,且到5时跳过输出)

    [root@localhost test20210728]# vim while-5+continue.sh 
    
    #!/usr/bin/bash
    i=0
    while [ $i -lt 10 ];do
      i=$((i+1))
      if [ $i -eq 5 ];then
        continue
      fi
      echo -n $i" "
    done
    echo

    查看运行结果:

    [root@localhost test20210728]# sh while-5+continue.sh 
    1 2 3 4 6 7 8 9 10

     

    四、until介绍

    和while正好相反,until是条件为假时开始执行,条件为真停止执行

     

    五、until语法

    until [ condition ]  #注意,条件为假until才会循环,until停止循环
        do
            commands代码块
    done

     

    六、until案例

    1、until实现输出10到20

    [root@localhost test20210729]# vim until_test.sh
    
    #!/usr/bin/bash
    
    num=10
    until [ $num -gt 20 ]
      do
        echo $num
        num=$((num+1))
    done

    查看运行结果:

    [root@localhost test20210729]# sh until_test.sh 
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

     

  • 相关阅读:
    vi—终端中的编辑器
    CSS Selector
    转: 通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
    WMI入门教程之WMI中的类在哪里?
    WMI测试器
    WMI Explorer操作 和 powershell命令
    C++ WMI获取系统硬件信息(CPU/DISK/NetWork etc)
    使用C++进行WMI查询的简单封装
    ubuntu版本信息查看
    win10 修改文件夹右击默认打开程序
  • 原文地址:https://www.cnblogs.com/mrwhite2020/p/15017987.html
Copyright © 2020-2023  润新知