bash if 语句中的两个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11370211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:23:28  来源:igfitidea点击:

Two conditions in a bash if statement

bashif-statementconditional-statements

提问by Andrzej Florek

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

我正在尝试编写一个脚本来读取两个选项,如果它们都是“y”,我希望它说“测试完成!” 如果其中一个或两个不是“y”,我希望它说“测试失败!”

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

但是当我用“y”回答两个问题时,它说“测试失败!” 而不是“测试完成!”。当我用“n”回答两个问题时,它表示“测试完成!”

What have I done wrong?

我做错了什么?

回答by abhshkdz

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

上述语句在choice!='y'和时为真choice1!='y',因此程序正确打印“测试完成!” .

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

回答by idz

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 noty will "Test Done !" be printed.

该程序正在按照您的要求执行操作。你说:“如果第一志愿不等于‘Y’和第二志愿不等于‘Y’然后打印‘测试完成’,否则打印‘测试失败’ - !所以只有两个选择是ÿ意志“测试完成!”被打印出来。

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.

我把!=不等于改为==等于。现在只有当你对两个问题都回答“y”时才会“测试完成!” 被打印。

回答by anonimous

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

胡言乱语泛滥。(;

回答by Christian Stieber

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

回答by jlehr

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

回答by Skippy Fastol

Try:

尝试:

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

回答by PasteBT

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

回答by cdk

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

测试两个选项是否都不是'y',因此当两个选项都为'y' 时,该语句为假并且您的程序正确打印“测试失败”。