LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-17-2009, 02:11 AM   #1
bekiricli
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Rep: Reputation: 0
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
 
Old 02-17-2009, 02:27 AM   #2
Udi
Member
 
Registered: Jan 2009
Posts: 165

Rep: Reputation: 44
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
 
Old 02-17-2009, 02:41 AM   #3
bekiricli
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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
 
Old 02-17-2009, 02:48 AM   #4
Udi
Member
 
Registered: Jan 2009
Posts: 165

Rep: Reputation: 44
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
 
Old 02-17-2009, 02:53 AM   #5
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
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.
 
Old 02-17-2009, 03:04 AM   #6
bekiricli
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 0
I got it working..!
Both helps are greatly appreciated..
Thanks a lot for your time..

Best,
Bekir
 
Old 02-17-2009, 05:43 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by Maligree View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with .procmailrc & regular expressions linuxmaveric Programming 5 06-11-2008 05:06 PM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Japanese canna won't work : Warning: かな漢字変&am OrganicOrange84 Debian 3 06-30-2005 02:28 PM
Phục hồi dữ liệu bị mất???, cứ pollsite General 1 06-27-2005 12:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration