Two conditions in if

淺唱寂寞╮ 提交于 2020-01-01 23:52:33

问题


I'm trying to write a script which will read two choices, and if both of them will be "y" I want it to say "Test Done!" and if one or both of them won't be "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 I've 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:


What if you tried

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



回答8:


this line

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

tests if both choices ARENT 'y', so when both choices ARE 'y', the statement is false and your program correctly prints "Test Failed"



来源:https://stackoverflow.com/questions/11370211/two-conditions-in-if

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!