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'
    
  • 相关阅读:
    linux
    day01-02
    测试基础
    cookie session
    多表表与表关系 增删改查 admin
    连接数据库 创建表 字段和参数 增删改查
    LeetCode OJ:Triangle(三角形)
    LeetCode OJ:Unique Paths II(唯一路径II)
    LeetCode OJ:Unique Paths(唯一路径)
    使用双栈实现一个队列
  • 原文地址:https://www.cnblogs.com/yangtao416/p/14811958.html
Copyright © 2011-2022 走看看