Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

XOR in an if test

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-20-2011
DougyC DougyC is offline
Registered User
 
Join Date: May 2010
Last Activity: 23 September 2014, 6:13 PM 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 Unix and Linux 01-20-2011
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 24 November 2014, 9:59 PM EST
Location: IN
Posts: 892
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 Unix and Linux 01-20-2011
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 7 April 2015, 6:37 PM EDT
Location: France
Posts: 2,931
Thanks: 81
Thanked 626 Times in 597 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 Unix and Linux 01-20-2011
DougyC DougyC is offline
Registered User
 
Join Date: May 2010
Last Activity: 23 September 2014, 6:13 PM 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 12:25 PM.