Two conditions in a bash if statement
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
if [ "$choice" != 'y' -a "$choice1" != 'y' ]; then
echo "Test Done !"
else
echo "Test Failed !"
fi
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
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.