美文网首页
shell的while循环

shell的while循环

作者: 哆来咪发都不会 | 来源:发表于2019-08-22 19:12 被阅读0次
    语法
    while 条件; do ...; done
    
    while死循环
    while :  
    while true  
    while 1  
    
    #!/bin/bash
    while :
    do
        read -p "Please input a number: " n
        if [ -z "$n" ]    #判断是否为空
        then
            echo "you need input sth."
            continue    #继续while循环,从read -p...开始继续循环
        fi
        n1=`echo $n|sed 's/[0-9]//g'`    #去掉数字,剩余内容赋值给变量n1
        if [ -n "$n1" ]    #变量n1是否不为空
        then
            echo "you can only input numbers."
            continue
        fi
        break    #跳出while循环
    done
    echo $n  
    
    break、continue、exit
    continue:结束本次循环,忽略continue后面的命令,直接进入下一次循环
    break:跳出循环,直接结束循环
    exit:直接退出脚本
    

    相关文章

      网友评论

          本文标题:shell的while循环

          本文链接:https://www.haomeiwen.com/subject/iyeosctx.html