user30772
user30772

Reputation: 166

bash if logical boolean string

I am not looking for a different way to accomplish the apparent intention. I'm looking to understand why this exact syntax 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 understand what 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.

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

Ok so here it is, with Nahuel's suggestion behaving how I had originally expected it to:

[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

Upvotes: 1

Views: 12129

Answers (3)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

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

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 break must be used only in a for or while loop, or in a select but it is missing in your post .

Upvotes: 1

V H
V H

Reputation: 8587

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

surely the most logical way to ensure input is 100% would be

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

        else

                 echo "Invalid entry... >$ans<"
         fi

Upvotes: 0

Rastislav Hasicek
Rastislav Hasicek

Reputation: 237

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 [[ "$ans" != "" ||  "$ans" != "y" ]];then 

it works fine.

Upvotes: 0

Related Questions