Operating System - Linux
1828219 Members
1992 Online
109975 Solutions
New Discussion

if with multiple conditions

 
Kanan
Occasional Advisor

if with multiple conditions

Could some one please correct the below if with multiple conditions
if [[ $smonth -le $temp ] && [ $syear -le $year ]] || [[ $smonth -gt $temp ]
&& [ $syear -le $year ]]; then

Thanking you in advance,
17 REPLIES 17
Peter Godron
Honored Contributor

Re: if with multiple conditions

Hi,
this depends on the logic you want to achieve !
But I would add another set of brackets around the whole statement, as at the moment you have :
if a || b ;then

Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.

So far you have only awarded points to 3 of 11 answers !
Hein van den Heuvel
Honored Contributor

Re: if with multiple conditions


You'll have to explain in words what you would like to accomplish.

As written the statement looks wrong both in actual writing and in intent.

Effectively the statement now says
if $syear le $year

It 'looks' like you can to compare for a date range. Now consider $temp=3 $year=2007.
And $smonth=12, $syear=2006.
Do you want that combination to pass or fail.

Yo may need to glue year and (zero-leading)month together before comparing (as string) or convert both to time-in-seconds.

Hope this helps some,
Hein van den Heuvel (at gmail dot com)
HvdH Performance Consulting

Oviwan
Honored Contributor

Re: if with multiple conditions

Hey

ksh:
if [ $smonth -le $temp -a $syear -le $year -o $smonth -gt $temp -o $syear -le $year ]
then

...

fi

Regards
Dennis Handly
Acclaimed Contributor

Re: if with multiple conditions

If you need something understandable, you can write it in C, assuming the above variables are integers:
if (( smonth <= temp && syear <= year || smonth > temp && syear <= year )); then

And as Hein say, this can be simplified to:
if [ (( syear <= year )) ]; then

>Oviwan: ... -a ... -o ... -o

Did you mean to have two -o?

Sandman!
Honored Contributor

Re: if with multiple conditions

How about the following expr:

if [ \( $smonth -le $temp -o $smonth -gt $temp \) -a $syear -le $year ]; then
Sandman!
Honored Contributor

Re: if with multiple conditions

Need to rectify my last post upon closer inspection. The parenthetical part will always be true so the IF can be written compactly as follows:

if [ $syear -le $year ]; then
Dennis Handly
Acclaimed Contributor

Re: if with multiple conditions

>Sandman!: The parenthetical part will always be true

That's what Hein said and I agreed.
Kanan
Occasional Advisor

Re: if with multiple conditions

Thanks all for the immediate responses. My requirement is to have multiple condition in a single if statemnet. I have tried various combinations, but not got the correct one. Please correct me to the right sysntax.

if [ && ] OR [ && ]

thanks..
Dennis Handly
Acclaimed Contributor

Re: if with multiple conditions

>Please correct me to the right syntax.

I did, if these are numbers and not strings:
if (( smonth <= temp && syear <= year || smonth > temp && syear <= year )); then

Otherwise you have to use:
if [[ X && Y || Z && W ]]; then

James R. Ferguson
Acclaimed Contributor

Re: if with multiple conditions

Hi Kanan:

# if [ \( condition1 -a condition2 \) -o \( condition3 -a condition4 \) ]; then
...

The '-a' means "and"; the '-o' means "or". The parentheses are escaped because they are special to the shell. See the manpages for 'test(1)'.

Regards!

...JRF...
Kanan
Occasional Advisor

Re: if with multiple conditions

Dennis, I have tried your suggestion but it gives syntax error. I believe u guys not understood my question. Below is more details.

I have 4 conditions. I want either 1&2 or 3&4 condition should match. All are string values.
[ condition 1 AND condition 2 ] OR [ Condition 3 AND condition 4 ]

Please help me.

Sandman!
Honored Contributor

Re: if with multiple conditions

Try the exemplary string expression evaluations below:

if [[ "$str1" = "$str2" && "$str3" ]] || [[ -z "$str4" && -n "$str5" ]]; then
statements
fi
James R. Ferguson
Acclaimed Contributor

Re: if with multiple conditions

Hi (again) Kanan:

> [ condition 1 AND condition 2 ] OR [ Condition 3 AND condition 4 ]

As I wrote before, this is a valid shell syntax for this:

# if [ \( condition1 -a condition2 \) -o \( condition3 -a condition4 \) ]; then
...

For example:

# if [ \( "${A}" = "${B}" -a "${C}" = "${D}" \) -o \( "${X}" != "${Y}" -a "${X}" != "${Z}" \) ]; then
...

Regards!

...JRF...
Kanan
Occasional Advisor

Re: if with multiple conditions

Hi James,

its giving syntax error again. FYI, my shell is ksh.
Dennis Handly
Acclaimed Contributor

Re: if with multiple conditions

All of these syntaxes are valid:
if (( smonth <= temp && syear <= year || smonth > temp && syear <= year )); then

if [[ X && Y || Z && W ]]; then

if [ \( condition1 -a condition2 \) -o \( condition3 -a condition4 \) ]; then

(In the above case, the use of () is redundant but may make it clearer.)

>I believe you guys not understood my question. I want either 1&2 or 3&4 condition should match. All are string values.

Sure we have. But this is the first time you said they were strings. This makes my (( )) solution invalid. Though in most cases if your variables have numeric values, they can be treated as numbers.

>Sandman: if [[ "$str1" = "$str2" && "$str3" ]] || [[ -z "$str4" && -n "$str5" ]]; then

This isn't valid. You only need one set of [[ ]]:
if [[ "$str1" = "$str2" && "$str9" = "$str3" || -z "$str4" && -n "$str5" ]]; then

>JRF:
if [ \( "${A}" = "${B}" -a "${C}" = "${D}" \) -o \( "${X}" != "${Y}" -a "${X}" != "${Z}" \) ]; then

>its giving syntax error again. FYI, my shell is ksh.

That syntax is perfectly valid. You need that exact spacing.

Using your original case:
if [[ ! ( $smonth > $temp ) && ! ( $syear > $year ) || $smonth > $temp && ! ( $syear > $year ) ]]; then

If these variables can be empty, you would need to quote them. If the variables contain a leading "-", you would have to prefix a "X".

Dennis Handly
Acclaimed Contributor

Re: if with multiple conditions

I forgot to mention, if you are having problems with our solutions, you should attach what you tried.
Dennis Handly
Acclaimed Contributor

Re: if with multiple conditions

It appears you can't use \( or \) within [[ ]], so my example should be:
if [[ ! ( $smonth > $temp ) && ! ( $syear > $year ) || $smonth > $temp && ! ( $syear > $year ) ]]; then

The "!" is needed because there are no <= or >= string compares.