欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Shell 编程 循环语句

程序员文章站 2022-04-14 20:45:48
本篇主要写一些 脚本循环语句的使用。 for 循环 指定次数 检查主机状态 while 循环 输出 到`10` bash !/bin/bash num=1 while [ $num le 10 ] do echo $num num= let num++ done bash !/bin/bash us ......

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
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 "感谢您的光临!欢迎下次再来!"