Bash 中“while”和“until”的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8287757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:14:57  来源:igfitidea点击:

Difference between "while" and "until" in Bash

bashwhile-loop

提问by Kyrol

What is the real difference between while and until ?

while 和 until 之间的真正区别是什么?

I'd like to know if it's possible to do an increasingly loop with "until" and a descending loop with while.

我想知道是否可以使用“直到”进行渐增循环和使用 while 进行递减循环。

Because I saw that if I do this

因为我看到如果我这样做

COUNTER=0
     while [ $COUNTER -lt 10 ]; do
         echo The counter is $COUNTER
         let COUNTER+=1 
     done

and this

和这个

COUNTER=20
     until [ $COUNTER -lt 10 ]; do
         echo COUNTER $COUNTER
         let COUNTER-=1
     done

they work well.

他们工作得很好。

But if I do the opposite, for example:

但如果我反其道而行之,例如:

COUNTER=20
     while [ $COUNTER -lt 10 ]; do
         echo COUNTER $COUNTER
         let COUNTER-=1
     done

the script doesn't end.

剧本并没有结束。

Does this means that we cannot do a reverse loop with a while in bash?

这是否意味着我们不能在 bash 中使用 while 进行反向循环?

回答by Oliver Charlesworth

whileruns the loop whilethe condition is true. untilruns the loop untilthe condition is true (i.e. whilethe condition is false).

while在条件为真运行循环。 until运行循环,直到条件为真(即条件为假)。

See http://www.gnu.org/software/bash/manual/bashref.html#Looping-Constructs.

请参阅http://www.gnu.org/software/bash/manual/bashref.html#Looping-Constructs

回答by Mandar Shinde

While Loopexecutes the block of code (enclosed in do...done) when the condition is trueand keeps executing that till the condition becomes false. Once the condition becomes false, the while loop is terminated.

条件为时,while 循环执行代码块(包含在do...done 中)并继续执行直到条件变为。一旦条件变为false,while 循环将终止。

Until Loopexecutes the block of code (enclosed in do...done) when the condition is falseand keep executing that till the condition becomes true. Once the condition becomes true, the until loop is terminated.

当条件为时,直到循环执行代码块(包含在do...done 中)并继续执行直到条件变为。一旦条件变为,直到循环终止。

COUNTER=20
while [ $COUNTER -lt 10 ]; do
    echo COUNTER $COUNTER
    let COUNTER-=1
done

On the very first occasion, the condition mentioned in while, i.e. [ $COUNTER -lt 10 ], holds false, so the block of code inside the while loop will not run at all.

第一次,while 中提到的条件,即[ $COUNTER -lt 10 ] 为假,因此 while 循环内的代码块根本不会运行。

回答by Vladp

They are opposite:

他们是相反的:

bool b;
while(b) = until(!b)

回答by Vilas

Btw:

顺便提一句:

COUNTER=20
while [ $COUNTER -lt 10 ]; do
    echo COUNTER $COUNTER
    let COUNTER-=1
done

This will not execute forever. Counter (20) is not less than 10 during the first evaluation of the loop condition so it will terminate immediately.

这不会永远执行。在循环条件的第一次评估期间,计数器 (20) 不小于 10,因此它将立即终止。

回答by Remario

Basically, while & until loops will only execute if the initial condition is met, think of while loops while, if the current condition is met only then will it work. However, until loops will wait until the condition is met. For example. The following will work because 5 is less than 10, the body of the loops increments the value until is exactly ten it will quit.

基本上,while & until 循环只会在满足初始条件时执行,想想 while 循环 while,如果满足当前条件,则它才会工作。但是,until 循环将一直等到条件满足。例如。以下将起作用,因为 5 小于 10,循环体将值递增,直到恰好 10 它将退出。

index=5
until [ $index -gt 10 ]
do
  echo $index
  let index+=1
done

This example would not work, because 5 is not initially greater than 10, only during the loop it becomes greater.

这个例子不起作用,因为 5 最初并不大于 10,只有在循环过程中它才会变得更大。

index=5
while [ $index -gt 10 ]
do
  echo $index
  let index+=1
done