1

I need a logic to implement the following logic in unix

if ( $a !="xyz" || $d !="abc" ) && ( $b= $c))
then
echo "YES WORKING"
fi

I tried below code not working

if [ [ [ $a != "xyz" ] -o [ $d != "abc" ] ] -a [ "$b" = "$c" ] ]
then
echo "YES WORKING"
fi

getting error as

:[ :] unexpected operator/operand

3
  • 1
    $a !="xyz" || $a !="abc" is always true Commented Jun 4, 2014 at 11:54
  • @fedorqui just now changed the logic Thanks for pointing that Commented Jun 4, 2014 at 12:09
  • @TimCastelijns getting error as :[ :] unexpected operator/operand Commented Jun 4, 2014 at 12:11

2 Answers 2

3

You can do something like this:

[ $a != "xyz" -o $d != "abc" ] && [ "$b" = "$c" ] && echo "YES WORKING"
Sign up to request clarification or add additional context in comments.

1 Comment

I forgot about -o. Your answer's better than mine with simpler shells. +1
2

Your logic should work easy in shells supporting [[ ]]:

if [[ ($a != "xyz" || $d != "abc") && $b = "$c" ]]; then
    echo "YES WORKING"
fi

Although there's a way for those that doesn't:

if ([ ! "$a" = "xyz" ] || [ ! "$d" = "abc" ]) && [ "$b" = "$c" ]; then
    echo "YES WORKING"
fi

But that's still inefficient since you'd be summoning subshells, so use { } but the syntax is a little ugly:

if { [ ! "$a" = "xyz" ] || [ ! "$d" = "abc" ]; } && [ "$b" = "$c" ]; then
    echo "YES WORKING"
fi

3 Comments

@Pranee What shell are you using? System? Perhaps you should consider the other methods as [[ ]] is only supported in modern shells like Zsh and Bash.
Also, are you running the lines alone, i.e. it's not included with other lines? If they work alone then perhaps it's a problem with other parts of the script.
your code worked but "$b" = "$c" not working may be because $b= "05/31/2014 " and $c="05/31/2014 "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.