欢迎光临
我们一直在努力

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

本站教程收集整理的这篇文章主要介绍了Bash脚本,在while循环中有多个条件,本站教程本站觉得挺不错的,现在分享给大家,也给大家做个参考。

我试图得到一个简单的while循环工作在bash使用两个条件,但尝试了许多不同的语法从各种论坛,我不能停止抛出一个错误。这里是我有:

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

我也试过:

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

…以及其他几个结构。我想要这个循环继续,而$ stats是> 300或$ stats = 0。

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

# 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 便宜美国vps can only bE interpreted as a variable
while (( stats > 300 || stats == 0 ))

一些注意事项:

>引用[[…]]和((…)中的参数扩展是可选的;如果未设置变量,-gt和-eq将假定值为0。>使用$是可选内部((…)),但使用它可以帮助避免无意的错误。如果没有设置stats,那么((stats> 300))将假定stats == 0,但是(($ stats> 300))将产生语法错误。

本站总结

以上是本站教程为你收集整理的Bash脚本,在while循环中有多个条件全部内容,希望文章能够帮你解决Bash脚本,在while循环中有多个条件所遇到的程序开发问题。

如果觉得本站教程网站内容还不错,欢迎将本站教程推荐给好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。