LinuxQuestions.org
Help answer threads with 0 replies.
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: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,017
Blog Entries: 5

Rep: Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787Reputation: 787
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: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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: Lubuntu
Posts: 19,176
Blog Entries: 4

Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
Moved to Programming
 
Old 06-14-2011, 06:37 AM   #8
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,401

Rep: Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 01:24 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration