Shell 编程 循环语句

2019-10-08 08:47:32来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Shell 编程 循环语句

CentOS-Logo

本篇主要写一些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 循环

  • 输出110
#!/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 "感谢您的光临!欢迎下次再来!"

原文链接:https://www.cnblogs.com/llife/p/11633411.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Shell 编程 条件语句

下一篇:Shell 编程 until语句