Bash for循环

与其他编程语言一样,bash shell脚本也支持for循环以执行重复性任务。它有助于在字符串中的一系列单词或数组中的元素上迭代一组特定的语句。例如,可以多次运行UNIX命令(或任务),也可以仅使用for循环读取和处理命令列表。

for循环的语法

可以通过两种方式在bash脚本上应用for循环。一种方法是for-in,另一种方法是C语言语法。以下是bash shell脚本中for循环的语法:

for variable in list  
do  
commands  
done

或者 –

for (( expression1; expression2; expression3 ))  
do  
commands  
done

for循环语句有一些关键点需要记住:

  • bash中for循环的每个块均以do关键字开头,后跟该块中的命令。for循环语句由done关键字关闭。
  • for循环的迭代时间取决于声明的列表变量。
  • 循环将从列表中选择一项,在循环中使用的变量上分配值。
  • dodone之间执行命令后,循环返回顶部,并从列表中选择下一项,然后重复整个过程。
  • 列表可以包含数字或字符串等,以空格分隔。

for循环示例

下面提供了一些for循环示例,以说明它们如何工作:

示例1 – for循环基础

Bash脚本:for-basic.sh

#!/bin/bash  
#This is the basic example of 'for loop'.  

learn="Start learning from yiibai.com"  

for learn in $learn  
do  
echo $learn  
done  

echo "Thank You."

执行上面示例代码,得到以下结果:
《Bash for循环》

示例2 – for循环范围

for循环读取范围,Bash脚本:for-range.sh

#!/bin/bash  
#This is the basic example to print a series of numbers from 1 to 10.  

for num in {1..10}  
do  
echo $num  
done  

echo "Series of numbers from 1 to 10."

执行上面示例代码,得到以下结果:
《Bash for循环》

示例3 – for循环增/减的范围

For循环读取带增/减的范围,可以通过添加两个点(..)并将值相加来增加或减少指定的值,例如{START..END..INCREMENT}。查看以下示例:

递增示例,脚本:for-increment.sh

#!/bin/bash  

#For Loop to Read a Range with Increment  

for num in {1..10..1}  
do  
echo $num  
done

执行上面示例代码,得到以下结果:
《Bash for循环》

递减示例,脚本:for-decrement.sh

#!/bin/bash  

#For Loop to Read a Range with Decrement  

for num in {10..0..1}  
do  
echo $num  
done

执行上面示例代码,得到以下结果:
《Bash for循环》

示例4 – for循环读取数组变量

可以使用for循环来迭代数组的值。

语法可以定义为:

array=(  "element1" "element 2" .  .  "elementN" )  

for i in "${arr[@]}"  
do  
echo $i  
done

对于array中的每个元素,将执行从“do”到“done”的语句或命令集。对于各个迭代,可以在循环内将每个元素作为i进行访问。请查看下面的示例,演示如何使用for循环遍历数组中的元素。

脚本文件:for-array.sh

#!/bin/bash  

#Array Declaration  
arr=( "Welcome","to","yiibai.com" )  

for i in "${arr[@]}"  
do  
echo $i  
done

执行上面示例代码,得到以下结果:

maxsu@yiibai:~/bashcode$ vi for-array.sh
maxsu@yiibai:~/bashcode$ chmod u+x for-array.sh 
maxsu@yiibai:~/bashcode$ ./for-array.sh 
Welcome,
to,
yiibai.com

示例5 – for循环读取字符串中的空白作为单词分隔符
语法可以定义如下:

#!/bin/bash  

for word in $str;  
do  
<Statements>  
done

在这里,str是指字符串。

对字符串的每个“单词”执行从dodone中的语句。查看以下示例:

脚本文件:for-string.sh

#!/bin/bash  
#For Loop to Read white spaces in String as word separators  

str="Let's start  
learning from yiibai.com."  

for i in $str;  
do  
echo "$i"  
done

执行上面示例代码,得到以下结果:
《Bash for循环》

示例6 – for循环以单词形式读取字符串中的每一行

语法定义如下:

#!/bin/bash  

for word in "$str";  
do  
<Statements>  
done

在这里,对字符串的“每行”执行从dodone的语句。参考以下示例:

Bash脚本文件:for-string2.sh

#!/bin/bash  
#For Loop to Read each line in String as a word  

str="Let's start  
learning from   
yiibai.com."  

for i in "$str";  
do  
echo "$i"  
done

执行上面示例代码,得到以下结果:
《Bash for循环》

注意:“for循环将字符串中的空白作为单词分隔符读取”与“For循环将字符串中的每一行作为单词读取”的唯一区别是字符串变量周围的双引号。

示例7 – for循环读取三表达式
三表达式语法是for循环的最常见语法。第一个表达式指的是初始化过程,第二个表达式指的是终止,第三个表达式指的是增量或减量。

查看以下示例,使用带for循环的三表达式打印110之间的数字:

Bash脚本文件:three-expression.sh

#!/bin/bash  
#For Loop to Read Three-expression  

for ((i=1; i<=10; i++))  
do  
echo "$i"  
done

执行上面示例代码,得到以下结果:
《Bash for循环》

示例8 – for循环与break语句

可以在for循环中使用break语句来终止循环。

Bash脚本文件:for-break.sh

#!/bin/bash  
#Table of 2  

for table in {2..100..2}  
do  
echo $table  
if [ $table == 20 ]; then  
break  
fi  
done

执行上面示例代码,得到以下结果:
《Bash for循环》

示例9 – for循环与continue语句

可以在for循环中使用continue语句来跳过特定条件下的特定语句。它告诉Bash停止执行循环的特定迭代并处理下一个迭代。

Bash脚本文件:for-continue.sh

#!/bin/bash  
#Numbers from 1 to 20, ignoring from 6 to 15 using continue statement"  

for ((i=1; i<=20; i++));  
do  
if [[ $i -gt 5 && $i -lt 16 ]];  
then  
continue  
fi  
echo $i  
done

执行上面示例代码,得到以下结果:
《Bash for循环》

示例10 – for无限循环

当bash的三个表达式中没有“开始,条件和增量”循环时,它将变为无限循环。要终止Bash中的无限循环,可以按Ctrl + C

Bash脚本文件:for-infinite.sh

#!/bin/bash  

i=1;  
for (( ; ; ))  
do  
sleep 1s  
echo "Current Number: $((i++))"  
done

执行上面示例代码,得到以下结果:

《Bash for循环》

        原文作者:Bash Shell教程
        原文地址: https://www.yiibai.com/bash/bash-for-loop.html
        本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
    点赞