LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   logical condition (https://www.linuxquestions.org/questions/programming-9/logical-condition-886067/)

nano2 06-13-2011 10:16 AM

logical condition
 
Hi

Using the following in bash doesn't produce the correct result.
can any one spot why ?
Code:

test= "A"
 if [ "$test" != "AB" ] ||  [ "$test" != "BC"]
then
    echo "No Match"
else
    echo "Match"
fi


druuna 06-13-2011 10:20 AM

Hi,

What output do you expect, 'cause it does produce the correct result (No Match).

Logic:
[ "$test" != "AB" ] || [ "$test" != "BC" ]
is
[ "A" != "AB" ] || [ "A" != "BC" ]
is
true || true
is
true
and thus the then part is executed.

Hope this helps.

MensaWater 06-13-2011 10:26 AM

No - Since you don't tell us what YOUR expected result is and what result you are getting that you think is wrong.

However, a couple of things that I notice:
1) You have a space between test= and the variable - that is not correct - you need to remove the space.
2) You have quotes around the variable ("A") which are unnecessary and might be treated literally in some contexts.
3) You do not have a space before your final right bracket which means the test is not recognized as such.
4) You not need quotes around $test in your test statements.

nano2 06-13-2011 02:01 PM

I am getting False result no match I have now tried to pass it in as command line

like so
Code:


test="$1"
if [  ! -z $test ]
then
    if [ "$test" != "AB"] ||  [ "$test" != "BC"]
    then
        echo "No Match"
    else
        echo "Exact Match"
    fi
fi

so when i pass in "AB" i still get the wrong match -can any one spot whats going on ?



Why can i not get the exact match

druuna 06-13-2011 02:16 PM

Hi,

That output is also correct:

Logic:

[ "$test" != "AB" ] || [ "$test" != "BC" ]

[ "AB" != "AB" ] || [ "AB" != "BC" ]

false || true

true

The then part (No match) is executed.

Try not to use negative checks (!=) try positive once (==) whenever possible. Have a look at these 2 examples:
Code:

#!/bin/bash

test="$1"
if [  ! -z $test ]
then
    if [ "$test" == "AB" ]
    then
        echo "first: Match"
    else
        echo "first: No Match"
    fi
fi


    if [ "$test" == "AB" ] ||  [ "$test" == "BC" ]
    then
        echo "second: Match"
    else
        echo "second: No Match"
    fi

Hope this helps.

MTK358 06-13-2011 08:18 PM

Quote:

Originally Posted by nano2 (Post 4384445)
I am getting False result no match I have now tried to pass it in as command line

like so
Code:


test="$1"
if [  ! -z $test ]
then
    if [ "$test" != "AB"] ||  [ "$test" != "BC"]
    then
        echo "No Match"
    else
        echo "Exact Match"
    fi
fi

so when i pass in "AB" i still get the wrong match -can any one spot whats going on ?

Why can i not get the exact match

So you want it to say "Exact Match" when the string is "AB" or BC"?

Basically, you're saying that it doesn't match if it's not "AB" or not "BC". If it's "AB", then it's not "BC", and you wrote that that means "No Match". Get it now?

XavierP 06-14-2011 03:06 AM

Moved to Programming

sundialsvcs 06-14-2011 06:37 AM

perl -e 'print ($test =~ /^(AB|BC)$/ ? "match" : "no match");'

so to speak ... it won't work quite as-writ but "you get the idea."

A "shell command" can be written in any programming language, thanks to "shebang" (#!), and you've probably got half-a-dozen of 'em out there on your computer right now.

rustek 06-22-2011 02:57 AM

Replace || with && and it will do what you want.

Russ


All times are GMT -5. The time now is 08:51 AM.