流程控制之while循环

一 语法

# 一、while语句结构:条件为真时,执行循环体代码
while 条件
do
	循环体
done

# 二、until语法结构:条件为假时,一直执行循环体代码,直到条件变为真
until 条件
do
	循环体
done

简单示例

[root@aliyun test]# cat a.sh 
#!/bin/bash

x=0
while (($x < 3))
do
    echo $x
    let x++
done

echo "================"

y=0
until (($y == 3))
do
    echo $y
    let y++
done

[root@aliyun test]# ./a.sh 
0
1
2
================
0
1
2
[root@aliyun test]# 

二 continue与break

continue:默认退出本次循环

break:默认退出本层循环

示例

[root@aliyun test]# cat a.sh 
#!/bin/bash

x=0
while (($x < 10))
do
    if (($x == 2));then
        let x++
        continue
    fi
   
    if (($x == 7));then
        break
    fi
    echo $x
    let x++
done

echo "================"

y=0
until (($y == 10))
do
    if (($y == 2));then
        let y++
        continue
    fi
   
    if (($y == 7));then
        break
    fi

    echo $y
    let y++
done
[root@aliyun test]# ./a.sh 
0
1
3
4
5
6
================
0
1
3
4
5
6
[root@aliyun test]# 

三 案例

案例1:监控web页面状态信息, 失败3次, 表示网站出现问题需要进行报警

[root@aliyun test]# cat f.sh 
#!/bin/bash
timeout=3
fails=0
success=0
url=$1

while true
do
    echo "=====>$fails"

    if [ $fails -eq 3 ]
    then
        echo "fails值等于3代表要进行第4次尝试,证明页面前访问3次均失败"
        break
    fi

    wget --timeout=$timeout --tries=1 http://$url -q

    if [ $? -ne 0 ]
    then
       let fails++
    else
       echo "页面访问成功"
       break
    fi

done

[root@aliyun test]# ./f.sh www.egon.com.cn
=====>0
=====>1
=====>2
=====>3
fails值等于3代表要进行第4次尝试,证明页面前访问3次均失败
[root@aliyun test]# 

案例2:猜数字

# 补充知识
    	方法一: 通过random变量产生随机数  (0-32768)
	    echo $RANDOM
	    
        方法二: 通过openssl命令产生随机数
	    openssl rand -base64 10
	    
        方法三: 通过时间信息获取随机数
	    date +%S%N
	    
        方法四: 通过一个特殊设备文件生成随机数
        head -c9 /dev/urandom|cksum
	    tr -dc 0-9 < /dev/urandom | head -c8
	    
        方法五: 利用UUID文件生成随机数
	    cat /proc/sys/kernel/random/uuid

# 代码实现
[root@egon shell]# cat guess_age.sh 
#!/bin/bash

num=`echo $((RANDOM%100+1))`  

count=0
while :
do
    [ $count -eq 3 ] && echo "猜的次数超过3次,退出" && exit
    read -p "请输入[1-100]之间的一个数字:" x
    [[ ! $x =~ ^[0-9]+$ ]] && echo "必须输入数字" && continue
    if [ $x -gt $num ];then
        echo "猜大了"
    elif [ $x -lt $num ];then
        echo "猜小了"
    else
        echo "猜对了"
        break
    fi
    let count++
done

案例3:while循环

[root@egon ~]# cat login.sh 
#!/bin/bash
while :  # 冒号代表永远为真,无限循环
do
    read -p 'please input your name: ' name
    read -p 'please input your password: ' pwd
    if [ $name = 'egon' ] && [ $pwd = '123' ]
        then
            echo 'login sucessful'
            break # continue
    fi
done

案例4:until

[root@egon /]# cat 4.sh 
#!/bin/bash

i=0
until [ $i -gt 4 ]
do
    if [ $i == 2 -o $i == 3 ];then
        let i++
        continue
    fi

    echo $i
    let i++
done
[root@egon /]# . 4.sh 
0
1
4

案例5:while循环

[root@egon /]# cat 1.sh 
#!/bin/bash
i=1
while ((i<10))
do
    echo $i
    ((i++))
done
[root@egon /]# . 1.sh 
1
2
3
4
5
6
7
8
9

案例6:while循环

[root@egon /]# cat 2.sh 
#!/bin/bash

var1="AAA"
var2="BBB"
var3="CCC"
while :
do
    clear
    echo -e "1:${var1}
2:${var2}
3:${var3}"
    temp=$var1
    var1=$var2
    var2=$var3
    var3=$temp
    sleep 1
done

案例7:while和read实现逐行处理

[root@egon /]# cat 3.sh 
#!/bin/bash
i=0
while read line
do
    echo $i:$line
    let i++
done</etc/passwd

案例8:用while循环+case来制作一个简单的菜单功能

#!/bin/bash
echo "script name: `basename $0`"
echo "version 1.0"
echo `date +%F_%H:%M:%S`
echo "Author: egon"
while read -p "(h for help): " var
do
    case $var in 
        p|P|cpu|CPU)
            echo -e "

"
            grep 'model name|cpu MHz|processor' /proc/cpuinfo |sort |uniq
            echo -e "

"
        ;;
        m|m|mem|MEM)
            echo -e "

"
            free
            echo -e "

"
        ;;
        d|D|disk|DISK)
            echo -e "

"
            df -Th
            echo -e "

"
        ;;
        h|H|help|HELP)
            echo -e "
	command	action

"
            for i in cpu mem disk
            do
            echo -e "	$i	${i}_info"
            done
            echo -e "	help	this help page.."
            echo -e "	quit	exit !!.."
            echo -e "

"
        ;;
        q|Q|quit|exit)
            exit
        ;;
        *)
            echo -e "
$var Enter Error...
"
    esac
done
原文地址:https://www.cnblogs.com/caodan01/p/14949015.html