zoukankan      html  css  js  c++  java
  • ⑤.shell 循环while

    语法示例:

    while 条件为真
    do
       echo ok
    done
    
    
    #!/bin/bash
    while true
    do
       echo ok
    done
    

    while循环数字

    #!/bin/bash
    a=1
    b=9
    while [ $a -lt 10 ]
    do
       sum=$((a + b))
       echo $a + $b = $sum
       let a++
       let b--
    done 
    

    while 逐行读取 根据文件创建用户

    准备用户文件
    [root@RainGod ~]# cat user.txt 
    yt01:test1
    yt02:test2
    yt03:test3
    
    
    脚本
    #!/bin/bash
    while read line 
    do
       user=`echo $line|awk -F: '{print $1}'`
       passwd=`echo $line|awk -F: '{print $2}'`
       id  $user &>/dev/null
       if [ $? -eq 0 ];then
          echo "$user is exiting"
       else
          useradd $user &>/dev/null
          echo $passwd |passwd --stdin $user
          echo "$user create ok"
       fi
    done<user.txt
    

    continue 终止本次循环 继续从头循环

    #!/bin/bash
    a=1
    while [ $a -lt 100 ]
    do
        if [ $a -eq 50 ];then
             echo 'eq 50 '
              let a++
              continue
        fi
        echo $a
       let a++
    done 
    

    break 跳出while循环 继续执行下面的语句

    #!/bin/bash
    a=1
    while [ $a -lt 100 ]
    do
        if [ $a -eq 50 ];then
             echo 'eq 50 '
              let a++
              break          
        fi
        echo $a
       let a++
    done 
    echo 'test'
    

    exit 结束脚本

    #!/bin/bash
    a=1
    while [ $a -lt 100 ]
    do
        if [ $a -eq 50 ];then
             echo 'eq 50 '
              let a++
              exit
        fi
        echo $a
       let a++
    done 
    echo 'test'
    
  • 相关阅读:
    SpringBoot 线程池配置 定时任务,异步任务
    使用Calendar类对时间进行处理
    单例模式的几种实现方式
    一些簡單的入門算法
    OO第四单元博客
    OO第三单元博客
    OO第二单元博客
    OO第一单元博客
    BUAA_OO_2020_Unit4_Wandy
    BUAA_OO_UNIT3_2020
  • 原文地址:https://www.cnblogs.com/yangtao416/p/14811958.html
Copyright © 2011-2022 走看看