LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2011, 10:16 AM   #1
nano2
Member
 
Registered: May 2007
Posts: 100

Rep: Reputation: 15
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
 
Old 06-13-2011, 10:20 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.

Last edited by druuna; 06-13-2011 at 10:29 AM.
 
Old 06-13-2011, 10:26 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
Old 06-13-2011, 02:01 PM   #4
nano2
Member
 
Registered: May 2007
Posts: 100

Original Poster
Rep: Reputation: 15
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
 
Old 06-13-2011, 02:16 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 06-13-2011, 08:18 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by nano2 View Post
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?
 
Old 06-14-2011, 03:06 AM   #7
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved to Programming
 
Old 06-14-2011, 06:37 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.

Last edited by sundialsvcs; 06-14-2011 at 06:38 AM.
 
Old 06-22-2011, 02:57 AM   #9
rustek
Member
 
Registered: Jan 2010
Location: Melbourne, IA, USA
Distribution: Ubuntu
Posts: 93

Rep: Reputation: 8
Replace || with && and it will do what you want.

Russ
 
  


Reply

Tags
bash scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] if condition shashv2 Programming 2 07-29-2010 09:52 AM
[SOLVED] If Condition with logical AND chirayu11 Linux - Newbie 2 07-29-2010 06:17 AM
Extending logical volume LogVol01 Insufficient allocatable logical exte swap space umeshsharma Linux - Newbie 4 06-22-2009 12:26 PM
if condition furqan_sindhu Linux - Newbie 5 09-22-2007 06:56 AM
using OR condition in if condition Fond_of_Opensource Linux - Newbie 2 10-20-2006 12:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration