Bash 脚本,while 循环中的多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15534595/
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 23:28:22  来源:igfitidea点击:

Bash scripting, multiple conditions in while loop

bashshellloopswhile-loop

提问by jake9115

I'm trying to get a simple while loop working in bash that uses two conditions, but after trying many different syntax from various forums, I can't stop throwing an error. Here is what I have:

我试图在使用两个条件的 bash 中获得一个简单的 while 循环,但是在尝试了来自各种论坛的许多不同语法之后,我无法停止抛出错误。这是我所拥有的:

while [ $stats -gt 300 ] -o [ $stats -eq 0 ]

I have also tried:

我也试过:

while [[ $stats -gt 300 ] || [ $stats -eq 0 ]]

... as well as several others constructs. I want this loop to continue while $stats is > 300or if $stats = 0.

...以及其他几个结构。我希望这个循环继续 while$stats is > 300或 if $stats = 0

回答by chepner

The correct options are (in increasing order of recommendation):

正确的选项是(按推荐的递增顺序):

# Single POSIX test command with -o operator (not recommended anymore).
# Quotes strongly recommended to guard against empty or undefined variables.
while [ "$stats" -gt 300 -o "$stats" -eq 0 ]

# Two POSIX test commands joined in a list with ||.
# Quotes strongly recommended to guard against empty or undefined variables.
while [ "$stats" -gt 300 ] || [ "$stats" -eq 0 ]

# Two bash conditional expressions joined in a list with ||.
while [[ $stats -gt 300 ]] || [[ $stats -eq 0 ]]

# A single bash conditional expression with the || operator.
while [[ $stats -gt 300 || $stats -eq 0 ]]

# Two bash arithmetic expressions joined in a list with ||.
# $ optional, as a string can only be interpreted as a variable
while (( stats > 300 )) || (( stats == 0 ))

# And finally, a single bash arithmetic expression with the || operator.
# $ optional, as a string can only be interpreted as a variable
while (( stats > 300 || stats == 0 ))

Some notes:

一些注意事项:

  1. Quoting the parameter expansions inside [[ ... ]]and ((...))is optional; if the variable is not set, -gtand -eqwill assume a value of 0.

  2. Using $is optional inside (( ... )), but using it can help avoid unintentional errors. If statsisn't set, then (( stats > 300 ))will assume stats == 0, but (( $stats > 300 ))will produce a syntax error.

  1. 引用里面的参数扩展[[ ... ]]并且((...))是可选的;如果变量没有被设置,-gt并且-eq将假设值0。

  2. $在 内部使用是可选的(( ... )),但使用它可以帮助避免意外错误。如果stats未设置,则(( stats > 300 ))假定为stats == 0,但(( $stats > 300 ))会产生语法错误。

回答by drewmm

Try:

尝试:

while [ $stats -gt 300 -o $stats -eq 0 ]

[is a call to test. It is not just for grouping, like parentheses in other languages. Check man [or man testfor more information.

[是对 的调用test。它不仅仅是用于分组,就像其他语言中的括号一样。检查man [man test了解更多信息。

回答by Lighthart

The extra [ ] on the outside of your second syntax are unnecessary, and possibly confusing. You may use them, but if you must you need to have whitespace between them.

第二种语法外部的额外 [ ] 是不必要的,并且可能会造成混淆。您可以使用它们,但如果必须,您需要在它们之间留有空格。

Alternatively:

或者:

while [ $stats -gt 300 ] || [ $stats -eq 0 ]