31

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?

Teymour
  • 1,832
  • 1
  • 13
  • 34
Andrzej Florek
  • 335
  • 1
  • 3
  • 8

8 Answers8

50

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
abhshkdz
  • 6,335
  • 1
  • 21
  • 31
3

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.

idz
  • 12,825
  • 1
  • 29
  • 40
  • Do we really need 7 (!) answers all saying that the logic is reversed? – Adam Rosenfield Jul 06 '12 at 22:11
  • 1
    @AdamRosenfield Nope! I only saw the others when I posted mine. I didn't remove because of the other 6 most of them do not mention the meaning of `!=` versus `==`. The OP is obviously fairly new to programming so just saying "Ya got your logic reversed" with minimal explanation is probably not going to help them learn. – idz Jul 06 '12 at 22:16
  • Yeah most of the other answers are not as helpful, I just picked on you because yours was the most recent. This is a textbook example of a [Fastest Gun in the West](http://meta.stackexchange.com/questions/9731/fastest-gun-in-the-west-problem) question. – Adam Rosenfield Jul 06 '12 at 22:27
1
if [ "$choice" != 'y' -a "$choice1" != 'y' ]; then
    echo "Test Done !"
else
    echo "Test Failed !"
fi
PasteBT
  • 2,128
  • 16
  • 17
1

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
Christian Stieber
  • 9,954
  • 24
  • 23
1

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
jlehr
  • 15,557
  • 5
  • 43
  • 45
1

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. (;

Dreamfire
  • 103
  • 2
  • 12
anonimous
  • 11
  • 2
0

Try:

if [[ "$choice" != 'y' && "$choice1" != 'y' ]]; then
    echo "Test Done!"
else
    echo "Test Failed!"
fi
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Skippy Fastol
  • 1,745
  • 2
  • 17
  • 32
  • 2
    Please consider adding some explanation or details to your answer. While it might answer the question, just adding a piece of code as an answer, doesn't per say help OP or future community members understand the issue or the proposed solution. – Maxim Jan 19 '20 at 09:30
0

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".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cdk
  • 6,698
  • 24
  • 51