bash 如果逻辑布尔字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12526909/
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
bash if logical boolean string
提问by user30772
I am notlooking for a different way to accomplish the apparent intention. I'm looking to understandwhy this exactsyntax is not working.
我不是在寻找不同的方式来实现明显的意图。我想了解为什么这种确切的语法不起作用。
[root@lvs ~]# while true;do
> echo "Would you like the script to check the second box ([y]n)?"
> read ans
> if [ "$ans" == "n" ];then
> echo
> echo "bye"
> exit
> elif [ "$ans" != "" -o "$ans" != "y" ];then
> echo "Invalid entry..."
> else
> break
> fi
> done
Would you like the script to check the second box ([y]n)? **"Should have continued"**
Invalid entry...
Would you like the script to check the second box ([y]n)? **"Should have continued"**
y
Invalid entry...
Would you like the script to check the second box ([y]n)? **"Correct behavior"**
alskjfasldasdjf
Invalid entry...
Would you like the script to check the second box ([y]n)? **"Correct behavior"**
n
bye
Here's a reference that's identical to so many others i found. I understandwhat it's doing, it's using the non logical's for AND and OR when everything I've read said that it should be using logical bools.
这是一个与我发现的许多其他参考相同的参考。我明白它在做什么,当我读过的所有内容都说它应该使用逻辑布尔值时,它使用非逻辑的 AND 和 OR。
http://www.groupsrv.com/linux/about140851.html
http://www.groupsrv.com/linux/about140851.html
Ok so here it is, with Nahuel's suggestion behaving how I had originally expected it to:
好的,就是这样,Nahuel 的建议表现出我最初的预期:
[root@lvs ~]# while true;do
> echo "Would you like the script to check the second box ([y]n)?"
> read ans
> if [ "$ans" = "n" ];then
> echo
> echo "bye!"
> exit
> elif [ "$ans" != "" -a "$ans" != "y" ];then
> echo "Invalid entry..."
> else
> break
> fi
> done
Would you like the script to check the second box ([y]n)?
asdfad
Invalid entry...
Would you like the script to check the second box ([y]n)?
[root@lvs ~]# while true;do
> echo "Would you like the script to check the second box ([y]n)?"
> read ans
> if [ "$ans" = "n" ];then
> echo
> echo "bye!"
> exit
> elif [ "$ans" != "" -a "$ans" != "y" ];then
> echo "Invalid entry..."
> else
> break
> fi
> done
Would you like the script to check the second box ([y]n)?
y
[root@lvs ~]# while true;do
> echo "Would you like the script to check the second box ([y]n)?"
> read ans
> if [ "$ans" = "n" ];then
> echo
> echo "bye!"
> exit
> elif [ "$ans" != "" -a "$ans" != "y" ];then
> echo "Invalid entry..."
> else
> break
> fi
> done
Would you like the script to check the second box ([y]n)?
n
logout
采纳答案by Nahuel Fouilleul
The problem is that : [ "$ans" != "" -o "$ans" != "y" ] is always true because of the or and the negation. $ans cannot be equal to "" and to "y".
问题在于: [ "$ans" != "" -o "$ans" != "y" ] 由于 or 和否定,总是为真。$ans 不能等于“”和“y”。
Try replace these lines
尝试替换这些行
if [ "$ans" == "n" ];then
elif [ "$ans" != "" -o "$ans" != "y" ];then
by these
通过这些
if [ "$ans" = "n" ];then
elif [ "$ans" != "" -a "$ans" != "y" ];then
or these
或者这些
if [[ $ans == n ]];then
elif [[ $ans != "" && $ans != y ]];then
The easier is to do is a case:
更容易做的是一个案例:
case $ans in
y) echo "yes"
;;
n) echo "no"
;;
*)
;;
esac
also breakmust be used only in a foror whileloop, or in a selectbut it is missing in your post .
也break必须只在 afor或while循环中使用,或者在 aselect但它在您的帖子中丢失。
回答by V H
also logically its a flawed way of doing things. firstly using case would be best in this scenario, secondly you are looking for == n then stating if it is blank or not equal to yes - so although no is caught out in first if statement in theory it would still meet second criteria
从逻辑上讲,它也是一种有缺陷的做事方式。首先在这种情况下使用 case 是最好的,其次你正在寻找 == n 然后说明它是空白还是不等于是 - 所以尽管理论上第一个 if 语句中没有发现它仍然符合第二个条件
surely the most logical way to ensure input is 100% would be
确保输入 100% 的最合乎逻辑的方法肯定是
if [ "$ans" == "n" ];then
echo
echo "bye"
exit
elif [ "$ans" == "y" ];then
echo Yes
break;
else
echo "Invalid entry... >$ans<"
fi
回答by Rastislav Hasicek
I don't really understand, why do you use -o in the elif. I would use "||" or "OR" operator. When you use two conditions in if, you should use double [[ and ]]. So if you use:
我真的不明白,你为什么在 elif 中使用 -o。我会用“||” 或“或”运算符。当你在 if 中使用两个条件时,你应该使用 double [[ 和 ]]。所以如果你使用:
elif [[ "$ans" != "" || "$ans" != "y" ]];then
it works fine.
它工作正常。

