使用“和"在Bash while循环中 [英] Using "and" in Bash while loop

查看:55
本文介绍了使用“和"在Bash while循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,基本上这就是脚本的样子:

Okay, essentially this is what the script looks like:

echo -n "Guess my number: "
read guess

while [ $guess != 5 ]; do
echo Your answer is $guess. This is incorrect. Please try again.
echo -n "What is your guess? "
read guess
done

echo "That's correct! The answer was $guess!"

我要更改的是此行:

while [ $guess != 5 ]; do

对于这样的事情:

while [ $guess != 5 and $guess != 10 ]; do

在Java中,我知道和"是&&",但这似乎在这里不起作用.我是否可以使用while循环来解决这个问题?

In Java I know "and" is " && " but that doesn't seem to work here. Am I going about this the right way using a while loop?

推荐答案

bash中的[]运算符是调用test的语法糖,在man test中有记录. 或"由中缀-o表示,但是您需要一个和":

The [] operator in bash is syntactic sugar for a call to test, which is documented in man test. "or" is expressed by an infix -o, but you need an "and":

while [ $guess != 5 -a $guess != 10 ]; do

这篇关于使用“和"在Bash while循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆