bash si logique booléenne chaîne

Je suis pas la recherche d'une façon différente de faire l'intention apparente. Je suis à la recherche de comprendre pourquoi cette exacte syntaxe ne fonctionne pas.

[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

Voici une référence identique à tant d'autres que j'ai trouvé. Je comprendre ce qu'il fait, c'est à l'aide de la non logique du pour et ET OU quand tout ce que j'ai lu a dit qu'il devrait être à l'aide de logiques booléens.

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

Ok donc, ici, il est, avec Nahuel la suggestion de se comporter de la façon dont j'avais prévu à l'origine pour:

[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
  • BTW, == est un bug qui est malheureusement accepté par bash. Seulement = est la POSIX façon de tester l'égalité.
InformationsquelleAutor user30772 | 2012-09-21