The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to giv two conditions in IF statement..?? RRVARMA Shell Programming and Scripting 6 04-25-2008 09:33 AM
reduce the or conditions hitmansilentass Shell Programming and Scripting 8 05-03-2007 05:27 PM
multiple conditions in if/then grandtheftander UNIX for Dummies Questions & Answers 4 07-21-2006 01:58 PM
if statement with two conditions cin2000 Shell Programming and Scripting 1 01-23-2006 03:21 PM
if statement with two conditions -e, && yongho Shell Programming and Scripting 16 06-14-2005 04:46 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-28-2008
borobudur borobudur is offline
Registered User
  
 

Join Date: Jul 2008
Location: Geneva, Switzerland
Posts: 23
Two conditions in one if statement

I'm totally new with bash programming and I don't get it how to put two conditions in one if statement. My code looks like this:
Code:
h=`date +%k`
if [ [ $((h>9)) ] && [ $((h<21)) ] ]; then
$h is 10 but I don't get into my if statement. What's wrong here?
  #2 (permalink)  
Old 07-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The shell has an eerie set of syntax alternatives for conditionals.

The [ is not a traditional delimiter, but the name of a command. The way to use it for multiple expressions is like this:

Code:
if [ "$h" -gt 9 ] && [ "$h" -lt 21 ]; then
  ...
fi
A slightly newer (that is, post-1979 or so ...) syntax is

Code:
if [ "$h" -gt 9 -a "$h" -lt 21 ]; then
  ...
fi
The $((...)) syntax is much newer, and introduces proper arithmetic (including the > and < operators) but is not directly usable as a condition. It simply expands to 0 for false and 1 for true. But of course, you can mix and match:

Code:
if [ $((h > 9 && h < 21)) == 1 ]; then
  ...
fi
Finally, there is the [[ ... ]] conditional, which is probably what you are after:

Code:
if [[ $h > 9 ]] && [[ $h < 21 ]]; then
  ...
fi
The characters which make up the [[ delimiter cannot be separated by whitespace (to the best of my knowledge).
  #3 (permalink)  
Old 07-28-2008
krishmaths krishmaths is offline
Registered User
  
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 191
Try this:

Code:
if [ $h -gt 9 -a $h -lt 21 ]; then
  #4 (permalink)  
Old 07-28-2008
borobudur borobudur is offline
Registered User
  
 

Join Date: Jul 2008
Location: Geneva, Switzerland
Posts: 23
Oh boy! I think I'm to young for this. I'm from the Java generation, bash seems to be quite tricky.

These two seem to work. Is that really the same?

Code:
if [ $h -gt 9 -a $h -lt 21 ]; then
Code:
if [ "$h" -gt 9 ] && [ "$h" -lt 21 ]; then
  #5 (permalink)  
Old 07-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The double quotes are mainly for safety (good habit in case $h ends up containing an empty string by mistake, for example) and in theory, the && variant could create two external processes where -a would only create one. (It's theory because [ is probably handled internally in moderns shells, so there is no external process involved.)

Shell scripts are not parsed much, the syntax is more shallow than in many other scripting languages and this creates some complications, but also helps make the shell extremely versatile. The lack of standard, built-in arithmetic operators right from the start is another source of complexity in this case. POSIX attempts to fix some of the issues but historically, different shells have developed different extensions which have then created, as it were, even more different ways to skin a cat.
  #6 (permalink)  
Old 07-28-2008
borobudur borobudur is offline
Registered User
  
 

Join Date: Jul 2008
Location: Geneva, Switzerland
Posts: 23
Thank you guys for your help!
Sponsored Links
Closed Thread

Bookmarks

Tags
conditions if statement

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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



All times are GMT -4. The time now is 04:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0