XOR in an if test | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

XOR in an if test

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-20-2011
DougyC DougyC is offline
Registered User
 
Join Date: May 2010
Last Activity: 25 March 2014, 6:10 AM EDT
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
XOR in an if test

Hi All,

Quick question.

How do I perform an xor in an if test in bash?

ie I want to know if string a = string b or string c but not both.

I've come up with several variations on the following:


Code:
if [[ "$1" == "-r" ^ "-d" ]]
then

and


Code:
if [ "$1" == "-r" ] ^ [ "$1" ==  "-d" ]
then

but thats giving me syntax errors.

Thanks for any help.

Last edited by DougyC; 01-20-2011 at 04:47 AM..
Sponsored Links
    #2  
Old 01-20-2011
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 893
Thanks: 14
Thanked 182 Times in 173 Posts
Can you try something like..

Code:
if [[ "$1" = "-r" ]] || [[ "$1" =  "-d" ]]

To my knowledge there is no ^ operation in shell test/if conditions.
Sponsored Links
    #3  
Old 01-20-2011
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 1 April 2014, 12:03 PM EDT
Location: France
Posts: 2,927
Thanks: 80
Thanked 623 Times in 595 Posts
$1 can obviously not hold "-r" and "-d" at the same time ... not sure about what you want to do
why not to use a case statement ?


Code:
case $1 in
"-r")
do_whatever
;;
"-d")
do_something
;;
*)
echo "wrong option"
exit 1
;;
esac

    #4  
Old 01-20-2011
DougyC DougyC is offline
Registered User
 
Join Date: May 2010
Last Activity: 25 March 2014, 6:10 AM EDT
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
thanks both.

both work. My brain is getting a bit flaky I think.

And as ctsgnb pointed out my logic was flawed in that I didn't have any.

I'm blaming too much work and not enough sleep!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to check weather a string is like test* or test* ot *test* in if condition johnjerome Shell Programming and Scripting 5 02-18-2010 05:38 AM
Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d Max89 Shell Programming and Scripting 1 11-25-2009 01:08 PM



All times are GMT -4. The time now is 10:53 AM.