Shell中的循环结构

本文详细介绍了Shell中的循环结构,包括步进循环语句for、until循环、while循环、嵌套循环以及如何使用break和continue语句控制循环。通过实例演示了不同类型的循环语句用法,如处理数组、条件控制、计数器以及跳出循环的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

步进循环语句for

for循环是最简单,也是最常用的循环语句。for循环通常用于遍历整个对象或者数字列表。按照循环条件的不同,for循环语句可以分为带列表的for循环、不带列表的for循环以及类C风格的for循环。

带列表的for循环语句

带列表的for循环通常用于将一组语句执行已知的次数,其基本语法如下:

for variable in {list}
do
   statement1
   statement2
   ...
done

在上面的语法中,variable称为循环变量,list是一个列表,可以是一系列的数字或者字符串,元素之间使用空格隔开。do和done之间的所有的语句称为循环体,即循环结构中重复执行的语句。for循环体的执行次数与list中元素的个数有关。在带列表的for语句的执行时,Shell会将in关键字后面的list列表的第1个元素的值赋给变量variable,然后执行循环体;当循环体中的语句执行完毕之后,Shell会将列表中的第2元素的值赋给变量variable,然后再次执行循环体。当list列表中的所有的元素都被访问后,for循环结构终止,程序将继续执行done语句后面的其他的语句。

【例1】演示将数字列表作为循环条件列表的for语句的使用方法

#! /bin/bash

#for循环开始
for var in 1 2 3 4 5 6 7 8
do
   #依次输出列表中的数字
   echo "the number is $var"
done

运行以上程序:

[root@localhost ~]# ./test.sh
the number is 1
the number is 2
the number is 3
the number is 4
the number is 5
the number is 6
the number is 7
the number is 8

【例2】本例将【例1】进行改造,使用了简单的书写方法

#! /bin/bash

#使用省略的写法表示某个范围
for var in {
   
   1..8}
do
   echo "the number is $var"
done

运行以上程序

[root@localhost ~]# ./test.sh
the number is 1
the number is 2
the number is 3
the number is 4
the number is 5
the number is 6
the number is 7
the number is 8

Shell允许用户指定for语句的步长。当用户需要另外指定步长时,其基本语法如下:

for varibale in {start..end..step}
do
   statement1
   statement2
   ...
done

【例3】演示通过for循环,并配合步长来计算100以内奇数的和

#! /bin/bash
#定义变量,并赋初值为0
sum=0;
#for循环开始,设置起始数值为1,结束数值为100,步长为2
for i in {
   
   1..100..2}
do
   #将数累加
   let "sum+=i"
done
echo "the sum is $sum"

运行以上程序

[root@localhost ~]# ./test.sh
the sum is 2500

【例4】本例将1周中7天的名字作为列表条件,依次输出每天的名称

#! /bin/bash
#for循环开始
for day in {
   
   Mon Tue Wed Thu Fri Sat Sun}
do
   #输出循环变量的值
   echo "$day"
done

运行以上程序:

[root@localhost ~]# ./test.sh
{Mon
Tue
Wed
Thu
Fri
Sat
Sun}

【例5】通过ls命令的输出结果作为for循环的执行条件

#! /bin/bash

#使用ls命令的执行结果作为列表
for file in $(ls)
do
   #输出每个文件名
   echo "$file"
done

运行以上程序

[root@localhost ~]# ./test.sh
aaa
anaconda-ks.cfg
Desktop
Documents
Downloads
initial-setup-ks.cfg
Music
Pictures
Public
Templates
test.sh
Videos

【例6】使用通配符“*”作为条件列表

#! /bin/bash
#使用通配符作为列表条件
for file in *
do
   echo "$file"
done

运行以上程序

[root@localhost ~]# ./test.sh
aaa
anaconda-ks.cfg
Desktop
Documents
Downloads
initial-setup-ks.cfg
Music
Pictures
Public
Templates
test.sh
Videos

【例7】说明如何使用for循环逐个处理脚本的参数值

#! /bin/bash
#输出所有的参数
echo "$*"
#将参数列表作为条件
for arg in $*
do
   #依次输出各个参数值
   echo "${arg}"
done

运行以上程序

[root@localhost ~]# ./test.sh a b c d de f
a b c d de f
a
b
c
d
de
f
不带列表的for循环语句

在某些特殊情况下,for循环的条件列表可以完全省略,称为不带列表的for循环语句。如果没有为for循环提供条件列表,Shell将从命令行获取条件列表。不带列表的for循环语句的一般语法如下:

for variable
do
   statement1
   statement2
   ...
done

由于系统变量$@同样可以获取所有的参数,所以以上的语法等价于以下语法:

for variable in $@
do
   statement1
   statement2
   ...
done

也同样等价于以下语法:

for variable in $*
do
   statement1
   statement2
   ...
done

【例8】演示不带列表的for循环语句的使用方法

#! /bin/bash
#不带条件列表
for arg
do
   #输出每个参数
   echo "$arg"
done

运行以上程序

[root@localhost ~]# ./test.sh a b c d de f 
a
b
c
d
de
f
类C风格的for循环语句

类C风格的for循环语句的基本语法如下:

for ((expression1;expression2;expression3))
do
   statement1;
   statement2;
   ...
done

在上面的语法中,for循环语句的执行条件被2个圆括号包括起来。执行条件分为3个部分,由2个分号隔开,第1部分expression1通常时条件变量初始化的语句;第2部分expression2是决定是否执行for循环的条件。当expression2的值为0时,执行整个循环体;当expression2的值为非0时,退出for循环体。第3部分,即表达式expression3通常用来改变条件变量的值,例如递增或者递减等。
【例9】演示类C风格的for循环语句的使用方法

#! /bin/bash
#for循环开始
for (( i=1;i<5;i++))
do
   #输出循环变量i的值
   echo "$i"
done

运行以上程序

[root@localhost ~]# ./test.sh
1
2
3
4
使用for循环语句处理数组

使用for循环遍历数组非常方便。针对数组,Shell专门提供了一种特殊语法的for循环语句,其基本语法如下:

for variable in ${array[*]}
do
   statement1
   statement2
   ...
done

其中,变量variable是循环变量,in关键字后面的部分表示要遍历的数组,其中array表示数组的名称。在遍历数组的过程中,for循环语句会将每个数组元素的值赋给循环变量variable。因此,用户可以在循环体中对每个数组元素进行相应的操作。
【例10】演示通过for循环来遍历数组

#! /bin/bash
#定义数组
array=(Monday Tuesday Wednesday Thursday Friday Saturday Sunday)
#通过for循环遍历数组元素
for day in ${array[*]}
do
   #输出每个数组元素的值
   echo $day
done

运行以上程序

[root@localhost ~]# ./test.sh
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

until循环语句

until循环语句同样也存在于多种程序设计语言中。顾名思义,until语句的作用时将循环体重复执行,直到某个条件成立为止。恰当地使用until语句,可以收到事半功倍地效果。

until语句的基本语法

until循环语句的功能是不断地重复执行循环体中的语句,直至某个条件成立。until语句的基本语法如下:

until expression
do
   statement1
   statement2
   ...
done

在上面的语法中,expression是一个条件表达式。当该表达式的值不为0时,将执行do和done之间的语句;当expression的值为0时,将退出until循环结构,继续执行done语句后面的其它的语句。
【例11】演示until语句的使用方法

#! /bin/bash
#定义循环变量i
i=1
#当i的值小于9时执行循环
until [[ "$i" -gt 9 ]]
do
   #计算i的平方
   let "square=i*i"
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值