Operating System - Linux
1753330 Members
5207 Online
108792 Solutions
New Discussion юеВ

Re: if with multiple conditions

 
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.