Shell 编程 循环语句
本篇主要写一些shell
脚本循环语句的使用。
for 循环
指定次数
#!/bin/bash
for ((i=1;i<=10;i++))
do
echo $i
done
[root@localhost ~]# vim num.sh
[root@localhost ~]# chmod +x num.sh
[root@localhost ~]# ./num.sh
1
2
3
4
5
6
7
8
9
10
- 九九乘法口诀表
#!/bin/bash
result=0
for ((i=1;i<=9;i++))
do
for ((j=1;j<=$i;j++))
do
result=`expr $i \* $j`
echo -en "$j * $i = $result\t"
done
echo ""
done
[root@localhost ~]# vim 99.sh
[root@localhost ~]# ./99.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
遍历循环
#!/bin/bash
TMP=$(cat username.txt)
for USER in $TMP
do
echo "用户:$USER"
done
[root@localhost ~]# vim users.sh
[root@localhost ~]# chmod +x users.sh
[root@localhost ~]# vim users.txt
[root@localhost ~]# cat users.txt
zhangsan
lisi
wangwu
[root@localhost ~]# ./users.sh
用户:zhangsan
用户:lisi
用户:wangwu
- 添加用户
#!/bin/bash
TMP=$(cat users.txt)
for USER in $TMP
do
useradd $USER && echo "123456" | passwd --stdin $USER &> /dev/null
done
echo "添加的用户信息为:"
tail -$(cat users.txt | wc -l) /etc/passwd
for USER in $TMP
do
userdel -r $USER
done
echo "已删除添加的用户"
[root@localhost ~]# vim useradd.sh
[root@localhost ~]# chmod +x useradd.sh
[root@localhost ~]# ./useradd.sh
添加的用户信息为:
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
wangwu:x:1003:1003::/home/wangwu:/bin/bash
已删除添加的用户
- 检查主机状态
#!/bin/bash
TMP=$(cat ipadds.txt)
for IP in $TMP
do
ping -c 4 -i 0.2 -W 3 $IP &> /dev/null
if [ $? -eq 0 ]
then
echo "Host $IP is up"
else
echo "Host $IP is down"
fi
done
[root@localhost ~]# vim ping.sh
[root@localhost ~]# chmod +x ping.sh
[root@localhost ~]# vim ipadds.txt
[root@localhost ~]# cat ipadds.txt
127.0.0.1
192.168.28.129
[root@localhost ~]# ./ping.sh
Host 127.0.0.1 is up
Host 192.168.28.129 is down
while 循环
- 输出
1
到10
#!/bin/bash
num=1
while [ $num -le 10 ]
do
echo $num
#num=`expr $num + 1`
let num++
done
- 添加用户
#!/bin/bash
# useradd
num=1
while [ $num -le 10 ]
do
useradd stu$num && echo "123456" | passwd --stdin stu$num &> /dev/null
let num++
done
echo "添加的用户信息为:"
tail -10 /etc/passwd
# userdel
num=1
while [ $num -le 10 ]
do
userdel -r stu$num
let num++
done
echo "已删除添加的用户"
[root@localhost ~]# vim useradd10.sh
[root@localhost ~]# ./useradd10.sh
添加的用户信息为:
stu1:x:1001:1001::/home/stu1:/bin/bash
stu2:x:1002:1002::/home/stu2:/bin/bash
stu3:x:1003:1003::/home/stu3:/bin/bash
stu4:x:1004:1004::/home/stu4:/bin/bash
stu5:x:1005:1005::/home/stu5:/bin/bash
stu6:x:1006:1006::/home/stu6:/bin/bash
stu7:x:1007:1007::/home/stu7:/bin/bash
stu8:x:1008:1008::/home/stu8:/bin/bash
stu9:x:1009:1009::/home/stu9:/bin/bash
stu10:x:1010:1010::/home/stu10:/bin/bash
已删除添加的用户
- 猜数字游戏
RANDOM
值的范围为:0-32767
#!/bin/bash
key=$(expr $RANDOM % 1000)
times=0
while true
do
read -p "请输入数字(0-999):" num
let times++
if [ $num -eq $key ]
then
echo "猜对了!你猜了 $times 次。"
exit 0
elif [ $num -gt $key ]
then
echo "猜高了"
else
echo "猜低了"
fi
done
[root@localhost ~]# vim guess.sh
[root@localhost ~]# chmod +x guess.sh
[root@localhost ~]# ./guess.sh
请输入数字(0-999):500
猜低了
请输入数字(0-999):750
猜低了
请输入数字(0-999):875
猜高了
请输入数字(0-999):800
猜低了
请输入数字(0-999):850
猜高了
请输入数字(0-999):825
猜低了
请输入数字(0-999):835
猜低了
请输入数字(0-999):840
猜高了
请输入数字(0-999):838
猜对了!你猜了 9 次。
综合实例
- 购物系统
#!/bin/bash
# Shopping System V1.1
c1=手套
c2=外套
c3=裤子
c4=鞋子
p1=40
p2=500
p3=150
p4=200
count=0
total=0
echo "欢迎光临!物美价廉!欢迎选购!"
for ((i=1;i<=5;i++))
do
read -p "第 $i 家店,是否进入(y/n):" ack
if [ $ack = y ]
then
echo "商品如下:"
echo "1.$c1:¥$p1"
echo "2.$c2:¥$p2"
echo "3.$c3:¥$p3"
echo "4.$c4:¥$p4"
while true
do
read -p "是否购买(y/n):" ack
if [ $ack = y ]
then
read -p "请输入需要购买的商品序号:" num
case $num in
1)
echo "您购买了$c1"
let count++
total=`expr $total + $p1`
;;
2)
echo "您购买了$c2"
let count++
total=`expr $total + $p2`
;;
3)
echo "您购买了$c3"
let count++
total=`expr $total + $p3`
;;
4)
echo "您购买了$c4"
let count++
total=`expr $total + $p4`
;;
*)
echo "输入错误,购买失败"
esac
elif [ $ack = n ]
then
echo "您走出了第 $i 家店"
break
else
echo "输入错误,请重新输入"
fi
done
elif [ $ack = n ]
then
continue
else
echo "输入错误,请重新输入"
let i--
fi
done
echo "购物数量:$count"
echo "购物总价:$total"
echo "感谢您的光临!欢迎下次再来!"