linux12shell编程 -->流程控制之while循环

文章目录

流程控制之while循环

1 、语法

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

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

2、简单示例

[root@openvpn day4]# cat count.sh 
#! /bin/bash

count=0
while [ $count -lt 5 ];
do
    echo $count
    let count++
done

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

#! /bin/bash

count=0

until [ $count -gt 4 ];
do
    echo $count
    let count++
done

[root@openvpn]# ./a.sh 
0
1
2
3
4
================
0
1
2
3
4

二 、continue与break

1、区别

continue:默认退出本次循环

break:默认退出本层循环

2、示例

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

count=0
while [ $count -lt 5 ];
do
if [ $count -eq 4 ];then
        break
    fi
    echo $count
    let count++
done

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

#! /bin/bash

count=0

until [ $count -gt 4 ];
do
if [ $count -eq 4 ];then
    let conunt++ 
    continue
    fi
    echo $count
    let count++
    
done

[root@openvpn]# ./a.sh 
0
1
3
================
0
1
3

三 、案例

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

[root@openvpn day4]# cat url.sh 
#!/bin/bash
count=0
url=$1

while true
do
    curl $1 &>/dev/null

    if [ $? -eq 0 ];then
        echo "$1 is ok"
        break
    else
      
       let count++
    fi
    echo "false is ${count}"
    if [ $count -eq 3 ];then
         echo "$1 is down"
       break
    fi
done
[root@openvpn day4]# ./url.sh http://www.jd.com
http://www.jd.com is ok
[root@openvpn day4]# ./url.sh http://www.jd.col
false is 1
false is 2
false is 3
http://www.jd.col is down
false值等于3代表要进行第4次尝试,证明页面前访问3次均失败

案例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循环

while :     # 冒号代表永远为真,无限循环
while true  # 冒号代表永远为真,无限循环
[root@openvpn day4]# cat user.sh 
#! /bin/bash
count=0
while true
do
read -p "请输入正确的用户名:" user
read -p "请输入正确的密码:" passwd
    
    if [ $user = "mm" ] && [ $passwd = "123" ];then
        echo "login successful"

	while true
	do
            echo "0 退出" 
            echo "1 提款" 
            echo "2 转账" 
            echo "3 查询余额"
	    read -p "请输出操作编号:" choice
	    case $choice in
	    "0")
	        #exit
		break
		;;
	    
	    "1")
		echo "正在提款"
		;;
	    
	    "2")
		echo "正在转账"
		;;
	    
	    "3")
		echo "正在查询余额"
		;;
	     
	     * )
		echo "指令输入错误,请重新输入"
	     esac
	done
	break
    else
        echo "user error or passwd error"
	let count++
    fi
    if [ $count -eq 3 ];then
	echo "tries too many times"
	break
    
    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}\n2:${var2}\n3:${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来制作一个简单的菜单功能

[root@openvpn day4]# vim all.sh
#!/bin/bash
echo "script name: `basename $0`"
echo "version 1.0"
echo `date +%F_%H:%M:%S`
echo "Author: mm"
while read -p "(h for help): " var
do
    case $var in 
        p|P|cpu|CPU)
            echo -e "\n\n"
            grep 'model name\|cpu MHz\|processor' /proc/cpuinfo |sort |uniq
            echo -e "\n\n"
        ;;
        m|m|mem|MEM)
            echo -e "\n\n"
            free
            echo -e "\n\n"
        ;;
        d|D|disk|DISK)
            echo -e "\n\n"
            df -Th
            echo -e "\n\n"
        ;;
        h|H|help|HELP)
            echo -e "\n\tcommand\taction\n\n"
            for i in cpu mem disk
            do
            echo -e "\t$i\t${i}_info"
            done
            echo -e "\thelp\tthis help page.."
            echo -e "\tquit\texit !!.."
            echo -e "\n\n"
        ;;
        q|Q|quit|exit)
            exit
        ;;
        *)
            echo -e "\n$var Enter Error...\n"
    esac
done

上一篇:bash for loop in one line


下一篇:shell循环语句for