0

I'm trying to prompt the user to enter, say either "5" or "10" And then store it as a variable. And if they do not enter either of these two numbers, to keep prompting them until they do. I've tried to run the following code, but it doesn't seem to work.

read -p 'Please enter your number. (5 or 10) ' num_choice

while (( $num_choice != 5|10))

do

read 'Please enter \"5\" or \"10\"' num_choice

done

echo "You've chosen the number $num_choice"

Also, it seems like the block quotes don't allow me to use single spacing, nor indent I'm assuming I'm doing something wrong. I apologize, I'm rather new to scripting! Thank you!

0

You can't use 5|10 on the right hand side of a comparator. You have to "distribute" the comparison

num_choice=0
while (( num_choice != 5 && num_choice != 10 )) ; do
    read -p 'Please enter your number (5 or 10)' num_choice
done
echo "You've chosen $num_choice"

Note that you can still get errors if the user specifies a non-numeric string.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.