一、while和until
1.语法
#一、while语句结构: 条件为真时,执行循环语句
while
do
循环体
done
#二、until语法结构:条件为假时,一直执行循环体代码,直到条件为真
until
do
循环体
done
2.示例
[root@db04 /scripts/day06]# vim 1.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@db04 /scripts/day06]# sh 1.sh
0
1
2
=======
0
1
2
二、continue和break
1.含义
# 1.continue: 默认退出本次循环
# 2.break:默认退出本层循环
2.示例
[root@db04 /scripts/day06]# vim 3.sh
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@db04 /scripts/day06]# sh 3.sh
0
1
3
4
5
6
============
0
1
3
4
5
6
三、相关案例
1.案例一
#用户登录账号密码进行银行相关操作,账号密码错误3次则退出
[root@db04 /scripts/day06]# vim while02.sh
#!/bin/bash
name="jh"
passwd="jh123"
count=0
while true
do
read -p "请输入用户名:" inp_name
read -p "请输入密码:" inp_passwd
if [ $inp_name = $name ] && [ $inp_passwd = $passwd ];then
echo "登录成功"
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 "用户名或密码错误..."
let count++
fi
if [ $count -eq 3 ];then
echo "tries too many times"
break
fi
done
2.案例二
#监控web页面状态信息, 失败3次, 表示网站出现问题需要进行报警
[root@db04 /scripts/day06]# vim 4.sh
#!/bin/bash
count=0
while true
do
curl $1 &>/dev/null
if [ $? -eq 0 ];then
echo "web页面状态正常"
break
else
echo "web页面状态错,正在进行第${count}次尝试"
let count++
if [ $count -eq 3 ];then
echo "web页面错误,请检查连接是否正常"
break
fi
fi
done
[root@db04 /scripts/day06]# ./4.sh www.baidu.com
web页面状态正常
[root@db04 /scripts/day06]# ./4.sh www.dddddddddddds.com
web页面状态错,正在进行第0次尝试
web页面状态错,正在进行第1次尝试
web页面状态错,正在进行第2次尝试
web页面错误,请检查连接是否正常
3.案例三
# 补充知识
方法一: 通过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@db04 /scripts/day06]# vim 5.sh
#!/bin/bash
# Author:jh
# Time:2020-11-23 16:20:06
# Name:5.sh
# Version: 1.0
# Discription: To
num=`echo $((RANDOM%100+2))`
count=0
while :
do
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++
if [ $count -eq 3 ];then
echo "猜的次数超过${count}次,退出"
break
fi
done
[root@db04 /scripts/day06]# sh 5.sh
请输入[1-100]之间的数字:43
猜大了
请输入[1-100]之间的数字:34
猜大了
请输入[1-100]之间的数字:22
猜大了
猜的次数超过3次,退出
4.案例四
#while循环
[root@jh ~]# cat login.sh
#!/bin/bash
while : # 冒号代表永远为真,无限循环
do
read -p 'please input your name: ' name
read -p 'please input your password: ' pwd
if [ $name = 'jh' ] && [ $pwd = '123' ]
then
echo 'login sucessful'
break # continue
fi
done
5.案例五
#until循环
[root@jh /]# 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@jh /]# . 4.sh
0
1
4
6.案例六
#while循环
[root@jh /]# cat 1.sh
#!/bin/bash
i=1
while ((i<10))
do
echo $i
((i++))
done
[root@jh /]# . 1.sh
1
2
3
4
5
6
7
8
9
7.案例七
#while
[root@jh /]# 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
8.案例八
#while和read实现逐行处理
[root@jh /]# cat 3.sh
#!/bin/bash
i=0
while read line
do
echo $i:$line
let i++
done</etc/passwd
9.案例九
#用while循环+case来制作一个简单的菜单功能
#!/bin/bash
echo "script name: `basename $0`"
echo "version 1.0"
echo `date +%F_%H:%M:%S`
echo "Author: jh"
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 - "\n$var Enter Error...\n"
esac
done