本篇主要寫一些 腳本迴圈語句的使用。 for 迴圈 指定次數 檢查主機狀態 while 迴圈 輸出 到`10` bash !/bin/bash num=1 while [ $num le 10 ] do echo $num num= let num++ done bash !/bin/bash us ...
本篇主要寫一些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
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 "感謝您的光臨!歡迎下次再來!"