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!"
What I want to change is this line:
while [ $guess != 5 ]; do
To something like this:
while [ $guess != 5 and $guess != 10 ]; do
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?
The Bash logical (&&) operator is one of the most useful commands that can be used in multiple ways, like you can use in the conditional statement or execute multiple commands simultaneously.
&& strings commands together. Successive commands only execute if preceding ones succeed. Similarly, || will allow the successive command to execute if the preceding fails. See Bash Shell Programming.
"&&" is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors (or, more accurately, exits with a return code of 0). "\" by itself at the end of a line is a means of concatenating lines together.
There is no do-while loop in bash. To execute a command first then run the loop, you must either execute the command once before the loop or use an infinite loop with a break condition.
There are 2 correct and portable ways to achieve what you want.
Good old shell
syntax:
while [ "$guess" != 5 ] && [ "$guess" != 10 ]; do
And bash
syntax (as you specify):
while [[ "$guess" != 5 && "$guess" != 10 ]]; do
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With