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

わかりましたので、ここにあります。ナウエルの提案は、私が最初に期待していた方法で動作します。

[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 と否定のために常に true になることです。$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のみ使用する必要がありますが、投稿にはありません。forwhileselect

于 2012-09-21T08:55:15.633 に答える
0

なぜelifで-oを使うのかよくわかりません。「||」を使用します または「OR」演算子。if で 2 つの条件を使用する場合は、[[ と ]] を二重に使用する必要があります。したがって、次を使用する場合:

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

それは正常に動作します。

于 2012-09-21T08:54:30.867 に答える
0

また、論理的には、物事のやり方に欠陥があります。このシナリオでは、最初にケースを使用するのが最適です。次に、 == n を探してから、それが空白であるか、yes と等しくないかを示します。つまり、最初の if ステートメントで no が検出されますが、理論的には 2 番目の基準を満たします。

確かに、入力が 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 に答える