0

I am working on a small homework assignment that is asking me to write a script that asks for four words, and a user must type it exactly as it is echo.

My issue is that in my output, it is not giving me an echo for false statements. Everything I put in my if is true, and gives me the first approved echo statement. Is there an issue with the setting of variables, or the brackets and parenthesis? I've been trying to put them in all sorts of different variations throughout the if portion, but can't seem to get it to kick the false output. I also use shell check as my syntax check source, and it is showing the script as being functional. Any help would be appreciated, here is what I have written.

#!/bin/bash
varname1=even
varname2=odd
varname3=zero
varname4=negative

# Ask the user for one of four select words
echo "Type one of the following words:"
echo "even, odd, zero, negative"
read varword
if [[ ("$varword" -eq $varname1 ) || ("$varword" -eq $varname2 ) || ("$varword" -eq $varname3 ) || ("$varword" -eq $varname4 ) ]]
then
    echo "The approved word you have selected is $varword ."
else
    echo "The unapproved word you have selected is $varword . Please try again."
fi
0

Use = for string comparisons, not -eq.

if [[ ("$varword" = "$varname1" ) || ("$varword" = "$varname2" ) || ("$varword" = "$varname3" ) || ("$varword" = "$varname4" ) ]]

alternatively, use a regexp:

if [[ $varword =~ ^(even|odd|zero|negative)$ ]] ; then
0

Use the bash select builtin

# Ask the user for one of four select words
PS3="Select one of the words: "
select choice in even odd zero negative; do
    [[ -n $choice ]] && break
done
echo "The approved word you have selected is $choice ."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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