Andrzej Florek
Andrzej Florek

Reputation: 355

Two conditions in a bash if statement

I'm trying to write a script which will read two choices, and if both of them are "y" I want it to say "Test Done!" and if one or both of them isn't "y" I want it to say "Test Failed!"

Here's what I came up with:

echo "- Do You want to make a choice?"
read choice

echo "- Do You want to make a choice1?"
read choice1

if [ "$choice" != 'y' ] && [ "$choice1" != 'y' ]; then
    echo "Test Done!"
else
    echo "Test Failed!"
fi

But when I answer both questions with "y" it's saying "Test Failed!" instead of "Test Done!". And when I answer both questions with "n" it's saying "Test Done!"

What have I done wrong?

Upvotes: 33

Views: 99266

Answers (8)

cdk
cdk

Reputation: 6778

The line

if [ "$choice" != 'y' ] && [ "$choice1" != 'y' ]; then

tests if both choices aren't 'y', so when both choices are 'y', the statement is false and your program correctly prints "Test Failed".

Upvotes: 0

Skippy Fastol
Skippy Fastol

Reputation: 1775

Try:

if [[ "$choice" != 'y' && "$choice1" != 'y' ]]; then
    echo "Test Done!"
else
    echo "Test Failed!"
fi

Upvotes: 0

anonimous
anonimous

Reputation: 11

Another thought,

$ c1='y' ; c2='y' ; [[ ${c1} = 'y' ]] && [[ ${c2} = 'y' ]] && echo true || echo false  
true  
$ c1='n' ; c2='y' ; [[ ${c1} = 'y' ]] && [[ ${c2} = 'y' ]] && echo true || echo false  
false  
$ c1='n' ; c2='y' ; [[ ${c1} = 'y' ]] || [[ ${c2} = 'y' ]] && echo true || echo false  
true  
$ c1='n' ; c2='n' ; [[ ${c1} = 'y' ]] || [[ ${c2} = 'y' ]] && echo true || echo false  
false  
$  

Overflow of gibberish. (;

Upvotes: 1

idz
idz

Reputation: 12988

The program is doing exactly what you told it to do. You said "If the first choice is not equal to 'y' and the second choice is not equal to 'y' then print "Test Done !" otherwise print "Test Failed !" -- so only if both choices are not y will "Test Done !" be printed.

You probably meant:

echo "- Do You want to make a choice ?"
read choice

echo "- Do You want to make a choice1 ?"
read choice1

if [ "$choice" == 'y' ] && [ "$choice1" == 'y' ]; then
echo "Test Done !"
else
echo "Test Failed !"
fi

I changed != not equals to == equals. Now only if you answer "y" to both questions will "Test Done !" be printed.

Upvotes: 2

jlehr
jlehr

Reputation: 15597

You have your logic reversed; you're checking for != when you should be checking for ==. Try this:

if [ "$choice" == 'y' ] && [ "$choice1" == 'y' ]; then
    echo "Test Done !"
else
    echo "Test Failed !"
fi

Upvotes: 1

Christian Stieber
Christian Stieber

Reputation: 12496

You got the comparison logic backwards; from your description you wanted to say

if [ "$choice" = 'y' ] && [ "$choice1" = 'y' ]; then

I'm actually surprised that the && construct works, although on further inspection it probably should. Still, I would write it as

if [ "$choice" = 'y' -a "$choice1" = 'y' ]; then

Upvotes: 1

PasteBT
PasteBT

Reputation: 2198

if [ "$choice" != 'y' -a "$choice1" != 'y' ]; then
    echo "Test Done !"
else
    echo "Test Failed !"
fi

Upvotes: 1

abhshkdz
abhshkdz

Reputation: 6365

You are checking for the wrong condition.

if [ "$choice" != 'y' ] && [ "$choice1" != 'y' ];

The above statement is true when choice!='y' and choice1!='y', and so the program correctly prints "Test Done!".

The corrected script is

echo "- Do You want to make a choice ?"
read choice

echo "- Do You want to make a choice1 ?"
read choice1

if [ "$choice" == 'y' ] && [ "$choice1" == 'y' ]; then
    echo "Test Done !"
else
    echo "Test Failed !"
fi

Upvotes: 52

Related Questions