1. 数字1-9进行升序输出
[root@shell /scripts/shell-day31]# cat while-1.sh
#!/bin/bash
a=1
while [ $a -lt 10 ]
do
echo $a
let a++
done
[root@shell /scripts/shell-day31]# sh while-1.sh
1
2
3
4
5
6
7
8
9
2. 数字1-9进行升序和降序输出
[root@shell /scripts/shell-day31]# cat while-2.sh
#!/bin/bash
a=1
b=9
while [ $a -lt 10 ]
do
echo $a $b
let a++
let b--
done
[root@shell /scripts/shell-day31]# sh while-2.sh
1 9
2 8
3 7
4 6
5 5
6 4
7 3
8 2
9 1
3. 写个简单的两数相乘
[root@shell /scripts/shell-day31]# cat while-3.sh
#!/bin/bash
a=1
while [ $a -lt 10 ]
do
echo "$a x $a = $(( $a * $a ))"
a=$(( $a + 1 ))
done
[root@shell /scripts/shell-day31]# sh while-3.sh
1 x 1 = 1
2 x 2 = 4
3 x 3 = 9
4 x 4 = 16
5 x 5 = 25
6 x 6 = 36
7 x 7 = 49
8 x 8 = 64
9 x 9 = 81
while循环场景示例
1. 写一个创建10个不同日期的文件。
[root@shell /scripts/shell-day31]# cat while-4.sh
#!/bin/bash
a=1
while [ $a -lt 11 ]
do
date -s "2020/03/$a" &>/dev/null && touch $(date +%F).txt
let a++
done
ntpdate ntp.aliyun.com &>/dev/null
[root@shell /scripts/shell-day31]# sh while-4.sh
[root@shell /scripts/shell-day31]# ll
total 1228
-rw-r--r-- 1 root root 0 2020-03-01 00:00 2020-03-01.txt
-rw-r--r-- 1 root root 0 2020-03-02 00:00 2020-03-02.txt
-rw-r--r-- 1 root root 0 2020-03-03 00:00 2020-03-03.txt
-rw-r--r-- 1 root root 0 2020-03-04 00:00 2020-03-04.txt
-rw-r--r-- 1 root root 0 2020-03-05 00:00 2020-03-05.txt
-rw-r--r-- 1 root root 0 2020-03-06 00:00 2020-03-06.txt
-rw-r--r-- 1 root root 0 2020-03-07 00:00 2020-03-07.txt
-rw-r--r-- 1 root root 0 2020-03-08 00:00 2020-03-08.txt
-rw-r--r-- 1 root root 0 2020-03-09 00:00 2020-03-09.txt
-rw-r--r-- 1 root root 0 2020-03-10 00:00 2020-03-10.txt
2. 通过读入文件的方式,进行创建用户
[root@shell /scripts/shell-day31]# cat while-5.sh
#!/bin/bash
while read line
do
id $line &>/dev/null
if [ $? -eq 0 ];then
echo "$line 用户已经存在!无需再次创建!"
else
useradd $line &>/dev/null
if [ $? -eq 0 ];then
echo "用户${line}创建成功!"
else
echo "用户${line}创建失败!"
fi
fi
done < user.txt
[root@shell /scripts/shell-day31]# sh while-5.sh
aaaa 用户已经存在!无需再次创建!
abbb 用户已经存在!无需再次创建!
accc 用户已经存在!无需再次创建!
[root@shell /scripts/shell-day31]# cat user.txt
aaaa
abbb
accc
3. 通过读入的文件的方式进行创建用户,并设置密码 user:pass
[root@shell /scripts/shell-day31]# cat user.txt
baaa:ireghutiirtg
bbbb:riueghutight
bccc:rueigheriugu
[root@shell /scripts/shell-day31]# cat while-6.sh
#!/bin/bash
while read line
do
#User=$(echo $line | awk -F: '{print $1}')
#Pass=$(echo $line | awk -F: '{print $2}')
id ${line%%:*} &>/dev/null
if [ $? -eq 0 ];then
echo "用户${line%%:*}已经存在!"
else
useradd ${line%%:*} &>/dev/null && echo ${line#*:} | passwd --stdin ${line%%:*} &>/dev/null
if [ $? -eq 0 ];then
echo "用户${line%%:*}创建成功!密码也设置成功!"
else
echo "用户${line%%:*}创建失败!"
fi
fi
done < user.txt
[root@shell /scripts/shell-day31]# sh while-6.sh
用户baaa创建成功!密码也设置成功!
用户bbbb创建成功!密码也设置成功!
用户bccc创建成功!密码也设置成功!
[root@shell /scripts/shell-day31]# cat user.txt
baaa:ireghutiirtg
bbbb:riueghutight
bccc:rueigheriugu
4. 通过读入文件的方式,进行创建用户, user , 并设置24位随机密码 密码需有大小字母,数字,特殊符号组成。
需将用户和创建好的密码以 User: $user Pass: $pass 存在密码文件中,密码文件为400权限,该脚本只能用超级管理员执行。
[root@shell /scripts/shell-day31]# cat user.txt
aaaa
abbb
accc
[root@shell /scripts/shell-day31]# cat while-7.sh
#!/bin/bash
#1.判断当前执行脚本的用户是否为超级管理员
if [ ! $USER == "root" -o ! $UID -eq 0 ];then
echo "当前用户${USER}对此脚本没有执行权限!"
exit
fi
while read line
do
id $line &>/dev/null
if [ $? -eq 0 ];then
echo "用户${line}已经存在!无需再次创建!"
else
Pass=$(mkpasswd -l 24 -c 6 -C 6 -d 6 -s 6)
useradd $line &>/dev/null && echo $Pass |passwd --stdin $line &>/dev/null
if [ $? -eq 0 ];then
echo "用户${line}创建成功!密码也设置成功!密码存放在pass.txt文件中。"
echo -e "User: $line\tPass: $Pass" >>pass.txt && chmod 400 pass.txt
else
echo "用户${line}创建失败!"
fi
fi
done < user.txt
[root@shell /scripts/shell-day31]# sh while-7.sh
用户caaa创建成功!密码也设置成功!密码存放在pass.txt文件中。
用户cbbb创建成功!密码也设置成功!密码存放在pass.txt文件中。
用户cccc创建成功!密码也设置成功!密码存放在pass.txt文件中。
[root@shell /scripts/shell-day31]# ll pass.txt
-r-------- 1 root root 126 2020-03-07 16:06 pass.txt
[root@shell /scripts/shell-day31]# cat pass.txt
User: caaa Pass: c63%R['a<8VqvZ1*k7}L5UVa
User: cbbb Pass: n0o#8o;39B?(rbV50>Na?FNZ
User: cccc Pass: !tntc7{XAi8^|ZcC446$I>C3
5. 猜数字游戏
1. 生成一个1-100的随机数
2. 要求用户输入必须是整数
3. 判断用户输入的数字跟这个随机数进行对比,大了则提示猜的数字大了
4. 如果数字小了,则提示输入的小了,否则猜对了
5. 最后统计出总共猜了多少次,失败了多少次。
[root@shell /scripts/shell-day31]# cat while-8.sh
#!/bin/bash
#定义一个随机数
Ran=$(( $RANDOM % 100 + 1 ))
i=0
while true
do
read -p "欢迎体验猜数字游戏!请输入你猜的数字:" Num
if [[ ! $Num =~ ^[0-9]+$ ]];then
echo "你输入的不是一个整数!请重新输入!"
continue #结束当前循环的剩余代码,继续执行下一次循环
fi
#将随机数与猜的数字进行对比
if [ $Num -gt $Ran ];then
echo "你猜的数字大了!请往小了猜!"
elif [ $Num -lt $Ran ];then
echo "你猜的数字小了!请往大了猜!"
else
echo "恭喜你!猜对了"
break
fi
let i++
done
echo "你总共猜了$(($i + 1))次!失败了${i}次!"
[root@shell /scripts/shell-day31]# sh while-8.sh
欢迎体验猜数字游戏!请输入你猜的数字:50
你猜的数字小了!请往大了猜!
欢迎体验猜数字游戏!请输入你猜的数字:75
你猜的数字小了!请往大了猜!
欢迎体验猜数字游戏!请输入你猜的数字:87
你猜的数字小了!请往大了猜!
欢迎体验猜数字游戏!请输入你猜的数字:94
恭喜你!猜对了
你总共猜了4次!失败了3次!
6. 随机点菜脚本
[root@shell /scripts/shell-day31]# cat while-9.sh
#!/bin/bash
#定义函数菜单
menu() {
cat<<EOF
#######################
1. 夫妻肺片 48元 #
2. 五香牛肉 68元 #
3. 蒜泥黄瓜 28元 #
4. 糖醋鲤鱼 78元 #
5. 红烧排骨 98元 #
6. 红烧猪蹄 88元 #
7. 海鲜大咖 298元 #
8. 澳洲龙虾 998元 #
9. 结束点菜 #
#######################
EOF
}
>cai.txt
echo "欢迎来到老男孩大酒店!很高兴为你服务!"
while true
do
menu
read -p "请开始你的点餐:" Num
if [[ ! $Num =~ ^[1-9]$ ]];then
echo "你点菜!本酒店没有!请按照菜单点菜!"
continue
fi
case $Num in
1)
clear
echo "你点了一份夫妻肺片,价格48元!"
echo "夫妻肺片 48 元" >>cai.txt
;;
2)
clear
echo "你点了一份五香牛肉,价格68元!"
echo "五香牛肉 68 元" >>cai.txt
;;
3)
clear
echo "你点了一份蒜泥黄瓜,价格28元!"
echo "蒜泥黄瓜 28 元" >>cai.txt
;;
4)
clear
echo "你点了一份糖醋鲤鱼,价格78元!"
echo "糖醋鲤鱼 78 元" >>cai.txt
;;
5)
clear
echo "你点了一份红烧排骨,价格98元!"
echo "红烧排骨 98 元" >>cai.txt
;;
6)
clear
echo "你点了一份红烧猪蹄,价格88元!"
echo "红烧猪蹄 88 元" >>cai.txt
;;
7)
clear
echo "你点了一份海鲜大咖,价格298元!"
echo "海鲜大咖 298 元" >>cai.txt
;;
8)
clear
echo "你点了一份澳洲龙虾,价格998元!"
echo "澳洲龙虾 998 元" >>cai.txt
;;
9)
echo "你结束了点菜!你的菜品详细如下:"
awk '{print $1}' cai.txt |sort | uniq -c | sort -rn | awk '{print "你点了"$2"!""点了"$1"份!"}'
echo "你本次消费金额如下:"
awk '{print $2}' cai.txt |xargs | tr ' ' '+' | bc | awk '{print "总价格为:"$1"元!"}'
echo "本酒店实行先买单后吃饭规则!"
exit
esac
done