LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-11-2008, 03:37 AM   #1
tikit
Member
 
Registered: Feb 2008
Posts: 84

Rep: Reputation: 16
multiple if condition in bash


Hi,

how can I write this multiple condition in a bash script?

Code:
if ((a > 30) || ((a < 0) && (b > 30))) then ...
Thanks for your help.

tikit
 
Old 11-11-2008, 05:08 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
You have to nest a double parenthesis construct like this:
Code:
if (( a > 30 || (( a < 0 && b > 30 )) )) ; then
 
Old 11-11-2008, 05:09 AM   #3
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189

Rep: Reputation: 49
aargh...colucix got to it before I did.

Last edited by w3bd3vil; 11-11-2008 at 05:10 AM.
 
Old 11-11-2008, 05:11 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Quote:
Originally Posted by w3bd3vil View Post
aargh...colucix got to it before I did.
he he he...
 
Old 11-11-2008, 06:23 AM   #5
tikit
Member
 
Registered: Feb 2008
Posts: 84

Original Poster
Rep: Reputation: 16
Thanks. It works!.
 
Old 11-11-2008, 05:54 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993Reputation: 1993
You appear to be trying to do numeric comparisons, in which case you're using the wrong operators. See http://www.tldp.org/LDP/abs/html/comparison-ops.html
 
Old 11-12-2008, 01:34 PM   #7
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

Quote:
Originally Posted by chrism01 View Post
You appear to be trying to do numeric comparisons, in which case you're using the wrong operators. See http://www.tldp.org/LDP/abs/html/comparison-ops.html
not in the bash. See "man bash":
Code:
       ((expression))
              The  expression is evaluated according to the rules described below under ARITH-
              METIC EVALUATION.  If the value of the expression is non-zero, the return status
              is  0;  otherwise  the  return  status  is 1.  This is exactly equivalent to let
              "expression".
...
ARITHMETIC EVALUATION
       The  shell  allows  arithmetic expressions to be evaluated, under certain circumstances
       (see the let builtin command and Arithmetic Expansion).  Evaluation is done  in  fixed-
       width  integers with no check for overflow, though division by 0 is trapped and flagged
       as an error.  The operators and their precedence and associativity are the same  as  in
       the C language.
...
       <= >= < >
              comparison
...
       &&     logical AND
       ||     logical OR
Jan
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I create multiple variables from a list in Bash? Passions Programming 12 05-23-2009 05:23 PM
create multiple directories in bash? Count Zero Programming 19 07-06-2008 08:34 AM
bash: multiple evaluations per line jhwilliams Linux - Software 1 08-28-2007 04:44 PM
Bash programming .. multiple includes ?? michael_util Programming 2 08-09-2004 11:47 AM
Giving multiple words as argument for bash ivanatora Linux - General 5 01-07-2004 02:04 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration