• (十)while和until循环


    (1)while循环

    语法:当条件测试成立(真),执行循环体
    while 条件测试
    do
    循环体
    done

    1)while批量创建用户1

    从user.txt读取到的行数据赋予给变量user值

    #!/bin/bash
    while read user 
    do
            id $user &>/dev/null
            if [ $? -eq 0 ];then
                    echo "user $user is already exists!"
            else
                    useradd $user
                    if [ $? -eq 0 ];then
                            echo "user $user is created!"
                    fi
            fi
    done <user.txt
    
    cat user.txt
    jack01
    jack02
    jack03
    jack04
    

    2)while批量创建用户2

    while默认空格作为分割,非常适合处理文件

    #!/bin/bash
    while read line
    do
            {
            user=$(echo $line|awk '{print $1}')
            pass=$(echo $line|awk '{print $2}')
            id $user &>/dev/null
            if [ $? -eq 0 ];then
                    echo "user $user is already exists!"
            else
                    useradd $user
                    echo $pass | passwd --stdin $user &>/dev/null
                    if [ $? -eq 0 ];then
                            echo "user $user is created!"
                    fi
            fi
            }&
    done <$1
    wait
    echo "all ok....."
    
    # cat user2.txt 
    jack001 123
    jack002	123
    jack03	123
    jack04	123
    

    3)while对有空行文件的处理方式

    #!/bin/bash
    while read line
    do
            if [ ${#line} -eq 0 ];then
                    continue
            fi
            {
            user=$(echo $line|awk '{print $1}')
            pass=$(echo $line|awk '{print $2}')
            id $user &>/dev/null
            if [ $? -eq 0 ];then
                    echo "user $user is already exists!"
            else
                    useradd $user
                    echo $pass | passwd --stdin $user &>/dev/null
                    if [ $? -eq 0 ];then
                            echo "user $user is created!"
                    fi
            fi
            }&
    done <$1
    wait
    echo "all ok....."
    

    (2)until循环

    语法:当条件测试成立(假),执行循环体
    until 条件测试
    do
    循环体
    done

    #!/bin/bash
    ip=114.114.114.114
    until ping -c1 -W1 $ip &>/dev/null
    do
            sleep 1
    done
    echo "$ip is up"
    

    (3)for,while,until 循环ping比较

    #!/bin/bash
    for i in {2..209}
    do
            {
            ip=192.168.111.$i
            ping -c1 -W1  $ip &>/dev/null
            if [ $? -eq 0 ];then
                    echo "$ip is up"
            fi
            }&
    done
    wait
    echo "all done...."
    
    #!/bin/bash
    i=2
    while [ $i -le 254 ]
    do
            {
            ip=192.168.111.$i
            ping -c1 -W1  $ip &>/dev/null
            if [ $? -eq 0 ];then
                    echo "$ip is up"
            fi
            }&
            let i++
    done
    wait
    echo "all done...."
    
    #!/bin/bash
    i=2
    until [ $i -gt 254 ]
    do
            {
            ip=192.168.111.$i
            ping -c1 -W1  $ip &>/dev/null
            if [ $? -eq 0 ];then
                    echo "$ip is up"
            fi
            }&
            let i++
    done
    wait
    echo "all done...."
    

    (4)for,while,until 循环求1到100的和

    #!/bin/bash
    for i in {1..100}
    do
            #let sum=$sum+$i
            let sum+=$i
    done
    echo "sum:$sum"
    
    #!/bin/bash
    i=1
    sum=0
    while [ $i -le 100 ]
    do
            #let sum=$sum+$i
            let sum+=$i
            let i++
    done
    echo "sum:$sum"
    
    #!/bin/bash
    i=1
    until [ $i -gt 100 ]
    do
            #let sum=$sum+$i
            let sum+=$i
            let i++
    done
    echo "sum:$sum"
    

    总结:循环次数固定建议使用for循环,循环文件建议使用while循环,循环次数不固定建议使用while和until循环

  • 相关阅读:
    [原创]PostgreSQL Plus Advanced Server批量创建分区表写入亿级别数据实例
    [原创]从Oracle和Microsoft Sql Server迁移到PostgreSQL Plus Advanced Server
    [原创]PostgreSQL Plus Advince Server在 HA环境中一对多的Stream Replication配置(四)
    【译】x86程序员手册41-10.6 TLB(快表)测试
    【译】x86程序员手册40-10.5初始化的例子
    【译】x86程序员手册39-10.3切换到保护模式
    【译】x86程序员手册38-10.2实在址模式下的软件初始化
    【译】x86程序员手册37-第10章 初始化
    【译】x86程序员手册36-9.9异常汇总
    【译】x86程序员手册35-9.8异常条件
  • 原文地址:https://www.cnblogs.com/lovelinux199075/p/8911412.html
Copyright © 2020-2023  润新知