流程控制之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
# 99乘法表
[root@openvpn ~]# cat 9.sh
for((i=1;i<=9;i++))
do
for((j=1;j<=i;j++))
do
let "s=i*j"
echo -ne "$i*$j=$s\t"
done
echo ""
done
echo "end" #取消换行符
#或者
for((i=1;i<=9;i++))
do
for((j=1;j<=i;j++))
do
let "((i*j))"
echo -ne "$i*$j=$((i*j))\t"
done
echo ""
done
[root@openvpn ~]# ./9.sh
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
# 方式二
[root@db02 ~]# cat a.py
a = 1
while a < 10:
b = 1
while b <= a:
print("%s*%s=%s\t" % (a, b, a * b), end="") # end=""取消换行符,\t相当于tab键
b += 1
print("")
a += 1
print("")
[root@db02 ~]# yum -y install python3 #安装python3
[root@db02 ~]# python3.6 a.py #python3.6运行a.py
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
# 方式三
[root@db02 ~]# cat 1.sh
a=1
while (( $a < 10 )); do
b=1
while (( $b <= $a )) ; do
echo -en "$b*$a=$(( $a*$b ))\t" # (echo -e \t)相当于tab键, (echo -n)取消换行符
let b++
done
let a++
echo ""
done
[root@db02 ~]# sh 1.sh
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
# 99乘法表加边框
[root@openvpn ~]# cat 91.sh
#!/bin/bash
x=1
STA1="---------"
STR2="|"
while (( x <= 9 ))
do
str=""
y=1
z=$x
while (( $z > 0 ))
do
echo -n $STA1
let z--
done
echo ""
while (( $y <= $x ))
do
str=$str"$STR2 $x*$y=$(echo $x*$y|bc) "
let y++
done
echo $str" "$STR2
let x++
done
while (( $x > 1 ))
do
echo -n $STA1
let x--
done
echo ""
---------
| 1*1=1 |
------------------
| 2*1=2 | 2*2=4 |
---------------------------
| 3*1=3 | 3*2=6 | 3*3=9 |
------------------------------------
| 4*1=4 | 4*2=8 | 4*3=12 | 4*4=16 |
---------------------------------------------
| 5*1=5 | 5*2=10 | 5*3=15 | 5*4=20 | 5*5=25 |
------------------------------------------------------
| 6*1=6 | 6*2=12 | 6*3=18 | 6*4=24 | 6*5=30 | 6*6=36 |
---------------------------------------------------------------
| 7*1=7 | 7*2=14 | 7*3=21 | 7*4=28 | 7*5=35 | 7*6=42 | 7*7=49 |
------------------------------------------------------------------------
| 8*1=8 | 8*2=16 | 8*3=24 | 8*4=32 | 8*5=40 | 8*6=48 | 8*7=56 | 8*8=64 |
---------------------------------------------------------------------------------
| 9*1=9 | 9*2=18 | 9*3=27 | 9*4=36 | 9*5=45 | 9*6=54 | 9*7=63 | 9*8=72 | 9*9=81 |
---------------------------------------------------------------------------------
[root@openvpn day5]# cat 9.sh
#! /bin/bash
for i in {1..9}
do
for ((j=1;j<=i;j++))
do
let "(($i*$j))"
echo -ne "$i*$j=$(($i*$j))\t"
done
echo ""
done
二 、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
三 、案例
# 奇数和偶数判断
[root@openvpn day1]# cat num3.sh
#! /bin/bash
num=100
while (( $num >= 0));
do
number=`echo "$num % 2" | bc`
if [ $number -eq 1 ];then
echo "$num是奇数"
fi
let num--
done
[root@openvpn day1]# cat num4.sh
#! /bin/bash
num=100
while (( $num >= 0));
do
number2=`echo "$num % 2" | bc`
number3=`echo "$num % 3" | bc`
if [ $number2 -eq 0 ] && [ $number3 -eq 0 ];then
echo "$num是2和3公倍数"
fi
let num--
done
案例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