Linux Bash 有 do-while 循环吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27761297/
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
Does Linux Bash have a do-while loop?
提问by Willem Van Onsem
After some search on the Internet, it appears that Bash doesn't have a do-whileloop.
在 Internet 上进行了一些搜索后,Bash 似乎没有do-while循环。
Is this correct? Is there a reliable source to confirm this (the lack of evidence that there is a do-whileloop is not an argument that there isn't one, perhaps a statement is only unpopular)?
这样对吗?是否有可靠的来源来证实这一点(缺乏证据表明存在do-while循环并不是说不存在循环的论点,也许声明只是不受欢迎)?
Is it possible to define directives oneself and thus implement a do-whileloop? There is an algorithmic way to transform a do-while-loop in a while-loop, but this is notthe scope of this question.
是否可以自己定义指令从而实现do-while循环?有一种算法方法可以在 while循环中转换do- while循环,但这不是本问题的范围。
回答by rici
bash
(or Posix shells in general) don't have an explicit syntax for a post-test loop (commonly known as a "do-while" loop) because the syntax would be redundant. The while
compound statement allows you to write pre-test, post-test or mid-test loops, all with the same syntax.
bash
(或一般的 Posix shell)没有用于测试后循环(通常称为“do-while”循环)的显式语法,因为该语法是多余的。该while
复合语句可以让你写预试验,试验后或中试循环,都具有相同的语法。
Here's the semantics of a shell while
loop, from Posix:
这是while
来自Posix的 shell循环的语义:
The format of the while loop is as follows:
while循环的格式如下:
while compound-list-1
do
compound-list-2
done
The compound-list-1 shall be executed, and if it has a non-zero exit status, the while command shall complete. Otherwise, the compound-list-2 shall be executed, and the process shall repeat.
将执行复合列表 1,如果它具有非零退出状态,则 while 命令将完成。否则,应执行复合列表 2,并重复该过程。
A "compound list" is a sequence of commands; the exit status of a compound list is the exit status of the last commandin the list.
“复合列表”是一个命令序列;复合列表的退出状态是列表中最后一个命令的退出状态。
That means that you could think of a while
loop as being written as follows:
这意味着您可以将while
循环视为如下编写:
while
optional-pre-test-compound-list
condition
do
post-test-compound-list
done
That is, there is no requirement that the condition to be tested immediately follows the while keyword. So the equivalent to the C syntax:
也就是说,不要求待测试的条件紧跟在 while 关键字之后。所以等效于 C 语法:
do statements while (test);
is
是
while statements; test do :; done
The :
between the do and the done is required, because the shell grammar does not allow empty statements. Since :
is not a metacharacter, it must have whitespace or a metacharacter before and after it; otherwise, it would be parsed as part of the preceding or succeeding token. Since it's parsed as a command, it also needs a semicolon or newline after it; otherwise the done
is treated as an argument to :
.
该:
之间做和做是必需的,因为shell语法不允许空语句。由于:
不是元字符,所以它前后必须有空格或元字符;否则,它将被解析为前一个或后继令牌的一部分。由于它被解析为命令,因此它后面还需要一个分号或换行符;否则 将done
被视为 的参数:
。
回答by Elliott Frisch
No, bash has no do-while
loop. The manualsection 3.2.4.1 Looping Constructslists until
, while
and for
. Notethere is no do-while
.
不,bash 没有do-while
循环。该手册节3.2.4.1循环结构列表until
,while
和for
。注意没有do-while
.