欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > Shell编程之循环语句

Shell编程之循环语句

2025/8/10 2:34:43 来源:https://blog.csdn.net/m0_74860678/article/details/139783697  浏览:    关键词:Shell编程之循环语句

Shell编程之循环语句

  • 一、for 循环语句
    • for 语句的结构
    • for 语句应用示例
  • 二、while循环语句
    • while语句的结构
    • while 语句应用示例
  • 三、until循环语句
    • until语句结构
    • until 语句应用示例
  • 注意:

在Shell编程中,循环语句是一个非常重要的组成部分,它允许我们重复执行某段代码,直到满足某个条件为止。下面,我将向大家介绍Shell中的几种常见的循环语句:for循环、while循环和until循环。

一、for 循环语句

for 语句的结构

  • 读取不同的变量值,用来逐个执行同一个命令序列
  • for循环通常用于遍历列表(如数组或字符串列表)中的元素
for 变量名 in 取值列表
do命令序列
done
  • for 语句的执行流程:
  • 首先将列表中的第一个取值赋给变量,并执行 do…done 循环体中的命令序列;
  • 然后将列表中的第二个取值赋给变量,并执行循环体中的命令序列……
  • 依此类推,直到列表中的所有取值用完,最后将跳至 done 语句,表示结束循环
    在这里插入图片描述

for 语句应用示例

根据姓名列表批量添加用户和删除

# 用做测试的列表文件chenye
[root@localhost ~]# vi users.txt
chenye
dengchao 
zhangjie
# 批量添加用户的脚本
[root@localhost ~]# vi uaddfor.sh
#!/bin/bashULIST=$(cat /root/users.txt) 
for UNAME in $ULIST
douseradd $UNAMEecho "123456" | passwd --stdin $UNAME &>/dev/null 
done
[root@localhost ~]# . uaddfor.sh    //测试并确认执行结果
[root@localhost ~]# tail -3 /etc/passwd
chenye:x:1005:1005::/home/chenye:/bin/bash 
dengchao:x:1006:1006::/home/dengchao:/bin/bash 
zhangjie:x:1007:1007::/home/zhangjie:/bin/bash# 批量删除用户的脚本
[root@localhost ~]# vi udelfor.sh
#!/bin/bash
ULIST=$(cat /root/users.txt) 
for UNAME in $ULIST
douserdel -r $UNAME &>/dev/null 
done
# 测试并确认执行结果
[root@localhost ~]# . udelfor.sh
[root@localhost ~]# id chenye
id: chenye: no such user        # 提示无此用户

根据 IP 地址列表检查主机状态

[root@localhost ~]# vi ipadds.txt    //用做测试的列表文件
172.16.16.1
172.16.16.22
172.16.16.220
[root@localhost ~]# vi chkhosts.sh    //循环检查各主机的脚本
#!/bin/bash
HLIST=$(cat /root/ipadds.txt) 
for IP in $HLIST
doping -c 3 -i 0.2 -W 3 $IP &> /dev/null        # -c 发送包的数量;-i 发送 ping 包间隔;-W 超时时间if [ $? -eq 0 ] thenecho "Host $IP is up."elseecho "Host $IP is down."fi 
done

二、while循环语句

while语句的结构

  • 重复测试某个条件,只要条件成立则反复执行
  • while循环会一直执行循环体内的命令,直到指定的条件不再满足为止
while 条件测试操作
do命令序列
done

在这里插入图片描述

while 语句应用示例

批量添加规律编号的用户和删除

[root@bogon ~]# cat uaddwhile.sh
#!/bin/bashPREFIX="stu"
i=1
while [ $i -le 20 ]     
douseradd ${PREFIX}$iecho "123456" | passwd --stdin ${PREFIX}$i &> /dev/null let i++
done[root@bogon ~]# . uaddwhile.sh 
[root@bogon ~]# grep "stu" /etc/passwd | tail -3
stu18:x:1067:1067::/home/stu18:/bin/bash
stu19:x:1068:1068::/home/stu19:/bin/bash
stu20:x:1069:1069::/home/stu20:/bin/bash[root@bogon ~]# cat udelwhile.sh 
#!/bin/bash
PREFIX="stu"
i=1
while [ $i -le 20 ] 
douserdel -r ${PREFIX}$ilet i++
done
[root@bogon ~]# . udelwhile.sh
[root@bogon ~]# id stu20
id: stu20: no such user

猜价格游戏

[root@bogon ~]# cat pricegame.sh 
#!/bin/bash
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo "商品实际价格范围为 0-999,猜猜看是多少?" 
while true
doread -p "请输入你猜测的价格数目:" INT let TIMES++if [ $INT -eq $PRICE ] ; thenecho "恭喜你答对了,实际价格是 $PRICE" echo "你总共猜测了 $TIMES 次"exit 0elif [ $INT -gt $PRICE ] ; then echo "太高了!"elseecho "太低了!"fi
done

三、until循环语句

until语句结构

  • 重复测试某个条件,只要条件不成立则反复执行
  • until循环与while循环相反,它会一直执行循环体内的命令,直到指定的条件满足为止
until 条件测试操作
do命令序列
done

在这里插入图片描述

until 语句应用示例

计算 1~50 的和

[root@localhost ~]# cat sum1to50_until_v1.sh 
#!/bin/bash 
i=0;s=0
until [ $i -eq 50 ] 
dolet "i=$i+1";let "s=$s+$i" 
doneecho 'sum(1..50)='$s
# 在 i 的值小于 50 之前,每次循环 i 的值加 1,并且求出 s 的值
[root@localhost ~]# . sum1to50_until_v1.sh 
sum(1..50)=1275

为指定用户发送在线消息

[root@localhost ~]# cat until-user_online_to_write.sh 
#!/bin/bash 
username=$1
if [ $# -lt 1 ]; then        # 对脚本参数个数不满足条件的进行处理echo "Usage:`basename $0` <username> [<message>]"exit 1
fi
if grep "^$username:" /etc/passwd > /dev/null;then :        # 判断第一个参数是否为系统用户
elseecho "$username is not a user on this system." exit 2
fi
until who|grep "$username" > /dev/null;do        # 接收信息的用户必须为登录在线用户echo "$username is not logged on."sleep 600
done
shift;msg=$*        # shift 去除第一个参数;$*为所有参数的值
[[ "X"$msg == "X" ]] && msg="Are you ready ? $username"        # 判断$msg 是否为空,为空则赋值
echo "$msg" | write $username        # 通过 write 命令给用户发送消息

注意:

  • 在条件判断中,[ ]是测试命令的语法,用于比较整数或检查字符串。注意[ ]之间要有空格。
  • $(( ))是算术扩展的语法,用于进行整数计算。
  • 你可以使用break命令来提前退出循环,或者使用continue命令来跳过当前循环的剩余部分,直接进入下一次循环。

循环语句在Shell编程中非常有用,特别是当你需要重复执行某个任务时。通过合理地使用循环语句,你可以写出更加高效、简洁的Shell脚本。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词