1

不是在寻找一种不同的方式来实现明显的意图。我希望了解为什么这种确切的语法不起作用。

[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

这是一个与我发现的许多其他参考相同的参考。我理解它在做什么,当我读到的所有内容都说它应该使用逻辑布尔时,它使用非逻辑的 AND 和 OR。

http://www.groupsrv.com/linux/about140851.html

好的,就是这样,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
4

3 回答 3

1

问题是: [ "$ans" != "" -o "$ans" != "y" ] 因为 or 和否定,所以总是正确的。$ans 不能等于 "" 和 "y"。

尝试替换这些行

if [ "$ans" == "n" ];then 
elif [ "$ans" != "" -o "$ans" != "y" ];then 

通过这些

if [ "$ans" = "n" ];then 
elif [ "$ans" != "" -a "$ans" != "y" ];then 

或者这些

if [[ $ans == n ]];then 
elif [[ $ans != "" && $ans != y ]];then 

比较容易做的是一个案例:

case $ans in
  y) echo "yes"
  ;;
  n) echo "no"
  ;;
  *)
  ;;
 esac

break只能在 a fororwhile循环中使用,或者在 a 中使用,select但您的 post 中缺少它。

于 2012-09-21T08:55:15.633 回答
0

我真的不明白,你为什么在 elif 中使用 -o。我会使用“||” 或“或”运算符。当你在 if 中使用两个条件时,你应该使用 double [[ 和 ]]。因此,如果您使用:

    elif [[ "$ans" != "" ||  "$ans" != "y" ]];then 

它工作正常。

于 2012-09-21T08:54:30.867 回答
0

从逻辑上讲,它也是一种有缺陷的做事方式。在这种情况下,首先使用 case 最好,其次您正在寻找 == n 然后说明它是否为空白或不等于 yes - 所以尽管 no 在理论上第一个 if 语句中被捕获,但它仍然符合第二个标准

当然,确保输入 100% 的最合乎逻辑的方法是

 if [ "$ans" == "n" ];then
                 echo
                 echo "bye"
                 exit
         elif [ "$ans" == "y" ];then
                 echo Yes
                        break;

        else

                 echo "Invalid entry... >$ans<"
         fi
于 2012-09-21T09:13:26.777 回答