-->

Two conditions in a bash if statement

2020-08-09 05:34发布

问题:

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?

回答1:

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


回答2:

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.



回答3:

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


回答4:

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


回答5:

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


回答6:

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



回答7:

Try:

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


回答8:

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