Bash while循环

在本小节中,将演示如何在Bash脚本中使用while循环语句。

bash while循环可以定义为控制流语句,只要所应用的条件为真,该语句就允许重复执行给定的命令集。例如,可以运行多次echo命令,也可以仅逐行读取文本文件,然后使用Bash中的while循环处理结果。

Bash While循环语法

Bash while循环具有以下格式:

while [ expression ];  
do  
commands;  
multiple commands;  
done

仅当表达式(expression)包含单个条件时,以上语法才适用。

如果表达式中包含多个条件,则while循环的语法如下:

while [ expressions ];  
do  
commands;  
multiple commands;  
done

while循环单行语法可以定义为:

while [ condition ]; do commands; done  
while control-command; do Commands; done

while循环语句有一些关键要点:

  • 在执行命令之前检查条件。
  • 可以使用while循环来执行“for循环”的所有工作。
  • 只要条件评估为真,dodone之间的命令就会重复执行。
  • while循环的参数可以是布尔表达式。

如何工作

while循环是一个受限的输入循环,因此在执行while循环的命令之前要先检查条件。如果条件评估为真,则执行该条件之后的命令集。否则,循环终止,并且在done语句之后将程序控制权交给另一个命令。

While循环示例

以下是bash while循环的一些示例:

示例1. 单条件的While循环
在此示例中,while循环与表达式中的单个条件一起使用。这是while循环的基本示例,它将根据用户输入打印一系列数字。

脚本文件:while-basic.sh

#!/bin/bash  
#Script to get specified numbers  

read -p "Enter starting number: " snum  
read -p "Enter ending number: " enum  

while [[ $snum -le $enum ]];  
do  
echo $snum  
((snum++))  
done  

echo "This is the sequence that you wanted."

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

示例2. 有多个条件的While循环

以下是在表达式中具有多个条件的while循环示例。

脚本文件:while-basic2.sh

#!/bin/bash  
#Script to get specified numbers  

read -p "Enter starting number: " snum  
read -p "Enter ending number: " enum  

while [[ $snum -lt $enum || $snum == $enum ]];  
do  
echo $snum  
((snum++))  
done  

echo "This is the sequence that you wanted."

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

示例3. 无限While循环
无限循环是没有结束或终止的循环。如果条件始终评估为true,则将创建一个无限循环。循环将会连续执行,直到使用CTRL + C强行停止循环为止。

脚本文件:while-infinite.sh

#!/bin/bash  
#An infinite while loop  

while :  
do  
echo "Welcome to Yiibai."  
sleep 1s
done

也可以将上述脚本写成一行:

#!/bin/bash  
#An infinite while loop  

while :; do echo "Welcome to Yiibai."; done

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

在这里,我们使用了始终返回true的内置命令(:)。还可以使用内置命令true来创建无限循环,如下所示:

#!/bin/bash  
#An infinite while loop  

while true  
do  
echo "Welcome to Yiibai"  
done

上面bash脚本输出与上述无限脚本输出的结果相同。

注意:无限循环可以通过使用CTRL + C或在脚本内添加一些条件退出来终止。

示例4. While循环与Break语句
根据所应用的条件,可以使用break语句来停止循环。脚本文件:whilie-break.sh

#!/bin/bash  
#While Loop Example with a Break Statement  

echo "Countdown for Website Launching..."  
i=10  
while [ $i -ge 1 ]  
do  
if [ $i == 2 ]  
then  
    echo "Mission Aborted, Some Technical Error Found."  
    break  
fi  
echo "$i"  
(( i-- ))  
done

根据上面脚本,将循环分配为迭代十次。但是在八次迭代之后存在一个条件,该条件会中断迭代并终止循环。执行脚本后,显示如下输出。
《Bash while循环》

示例5. While循环与Continue语句

continue语句可用于在while循环内跳过特定条件的迭代。

脚本文件:while-continue.sh

#!/bin/bash  
#While Loop Example with a Continue Statement  

i=0  
while [ $i -le 10 ]  
do  
((i++))  
if [[ "$i" == 5 ]];  
then  
    continue  
fi  
echo "Current Number : $i"  
done  

echo "Skipped number 5 using Continue Statement."

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

示例6. C语言样式while循环

还可以在bash脚本中编写像在C编程语言中编写while循环一样。脚本文件:while-cstyle.sh

#!/bin/bash  
#While loop example in C style  

i=1  
while((i <= 10))  
do  
echo $i  
let i++  
done

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

《Bash while循环》

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