LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   if ( a == 5 && b == 6 ) like expressions (https://www.linuxquestions.org/questions/linux-newbie-8/if-a-%3D%3D-5-and-and-b-%3D%3D-6-like-expressions-705277/)

bekiricli 02-17-2009 02:11 AM

if ( a == 5 && b == 6 ) like expressions
 
Hi all,

I somehow couldn't find the "linux bash script" equivalent of this C expression. How do we bind several comparisons together in bash script?

Best,
Bekir

Udi 02-17-2009 02:27 AM

The 'test' command is used in bash for this. You can do 'man test' to see its syntax. The '[' command is equivalent to test:

if [ $a = 5 -a $b = 6 ]; then
echo "condition true"
else
echo "condition false"
fi

bekiricli 02-17-2009 02:41 AM

Thanks Udi, I know it is such a simple question..
But I still couldn't get it working with "test", I mean it is not clarified in the manual, how to bind several expressions together. Would you have time to write the example with "test"?

Regards,
Bekir

Udi 02-17-2009 02:48 AM

The same example with test is the same as the previous example:

if test $a = 5 -a $b = 6 ; then
echo "condition true"
else
echo "condition false"
fi


From the manual page:
EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true

Maligree 02-17-2009 02:53 AM

Quote:

how to bind several expressions together.
Code:

if [ $foo -eq 5 -a $boo -eq 10 ]; then
        echo "True.";
fi

-a is AND. The statement is true when $foo is equal 5 and $boo is equal 10.
Code:

if [ $foo -eq 5 -o $boo -eq 10 ]; then
        echo "True.";
fi

-o is OR. Statement is true for $foo = 5 or $boo = 10.

That's how I always use it. And it works. Maybe there's some other way to use AND/OR expressions but I don't care. This has worked for me.

Hope I helped.

bekiricli 02-17-2009 03:04 AM

I got it working..! :)
Both helps are greatly appreciated..
Thanks a lot for your time..

Best,
Bekir

i92guboj 02-17-2009 05:43 AM

Quote:

Originally Posted by Maligree (Post 3446567)
Code:

if [ $foo -eq 5 -a $boo -eq 10 ]; then
        echo "True.";
fi

-a is AND. The statement is true when $foo is equal 5 and $boo is equal 10.
Code:

if [ $foo -eq 5 -o $boo -eq 10 ]; then
        echo "True.";
fi

-o is OR. Statement is true for $foo = 5 or $boo = 10.

That's how I always use it. And it works. Maybe there's some other way to use AND/OR expressions but I don't care. This has worked for me.

Hope I helped.

It's the simplest way, I guess.

You can always concatenate two tests, which I use sometimes for the sake of clarity, but it's probably less efficient.

Code:

if [ $foo == 5 ] && [ $boo == 1 ]; then echo ok; fi
if [ $foo == 5 ] || [ $boo == 1 ]; then echo ok; fi



All times are GMT -5. The time now is 04:05 PM.