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

本文介绍了Bash脚本中条件表达式的使用,包括POSIX测试命令、双括号表达式以及逻辑运算符的组合。强调了引用变量的重要性,以防空值或未定义导致的错误。同时,讨论了不同方式下如何检查变量stats是否大于300或等于0,解释了在不同情况下的行为和潜在陷阱。
摘要由CSDN通过智能技术生成

正确的选项是(增加推荐的顺序排列):

# 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))

一些注意事项:

引用内部[[ ... ]]和((...))的参数扩展是可选的;如果变量没有设置,-gt和-eq将承担0.1

使用$里面((...))可选的值,但使用它可以帮助避免无意的错误。如果stats未设置,则((stats > 300))将假定为stats == 0,但(($stats > 300))将产生语法错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值