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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
IF Statement koti_rama Shell Programming and Scripting 3 04-29-2008 04:48 AM
If statement - How to write a null statement april Shell Programming and Scripting 3 04-16-2008 01:14 PM
using && in if statement .. jisha Shell Programming and Scripting 7 02-01-2008 07:52 AM
IF statement on a df -g hassanj UNIX for Advanced & Expert Users 1 12-18-2007 07:41 AM
If statement mariner UNIX for Advanced & Expert Users 4 12-16-2004 07:21 AM

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

Join Date: Sep 2006
Posts: 56
if statement

Hi,
how to write an if statement for values in between

if [$? -ne 0];then

fi

here I want to write an if statement if $? is inbetween 1 and 126
how to write this
if [$? inbetween 1 and 126];then
fi


I'd also like to know the difference between the following two if statements
if [$? -ne 0];then
fi

and
if [[$? -ne 0]];then
fi

Thanks for your time.
Ram
  #2 (permalink)  
Old 05-28-2008
mstephens mstephens is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
Quote:
Originally Posted by ramky79 View Post
Hi,
how to write an if statement for values in between

if [$? -ne 0];then

fi

here I want to write an if statement if $? is inbetween 1 and 126
how to write this
if [$? inbetween 1 and 126];then
fi


I'd also like to know the difference between the following two if statements
if [$? -ne 0];then
fi

and
if [[$? -ne 0]];then
fi

Thanks for your time.
Ram
Depends on which shell you are using. The "[" version of test is the original one and tends to be an external program. Newer shells have "[[" which is a built in one and thus more efficient. You might also have "((" which is specifically for numeric stuff which means that you can use normal numeric comparisons rather than -ne, -eq etc.

To do an "in between" test with any of the above you need to use AND or OR depending on which way round you do it. AND is && and OR is ||


e.g.

if [ $val -lt 127 ] && [ $val -gt 0 ]
then
......

fi



there might be niftier ways with some shells (like doing the && or || inside a single test ) but it depends what you are using.
  #3 (permalink)  
Old 05-28-2008
Dave Miller's Avatar
Dave Miller Dave Miller is offline
Registered User
  
 

Join Date: Dec 2007
Location: Northern NJ, USA
Posts: 151
We were posting at the same time.


Hmmm...
Quote:
Originally Posted by mstephens View Post
... AND is && and OR is ||
I was curious about those double ampersands and pipes I've seen as well.

Thanks.
  #4 (permalink)  
Old 05-28-2008
Dave Miller's Avatar
Dave Miller Dave Miller is offline
Registered User
  
 

Join Date: Dec 2007
Location: Northern NJ, USA
Posts: 151
For the in-between question:

if [ $? -ge 1 -a $? -le 126 ] ; then
...
fi


For the what's the difference question:
I'm kinda curious about that too.
  #5 (permalink)  
Old 05-28-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Quote:
Originally Posted by Dave Miller View Post
For the in-between question:

if [ $? -ge 1 -a $? -le 126 ] ; then
...
fi


For the what's the difference question:
I'm kinda curious about that too.
This is the Bourne shell syntax:

Code:
[ condition1 -a condition2 ]
[ condition1 -o condition2 ]
And this is the Korn shell syntax:

Code:
[wiki] condition1 && condition2 [/wiki]
[wiki] condition1 || condition2 [/wiki]
The -a and -o clauses can not be used inside the Korn shell-specific test statement.

Regards
  #6 (permalink)  
Old 05-28-2008
Dave Miller's Avatar
Dave Miller Dave Miller is offline
Registered User
  
 

Join Date: Dec 2007
Location: Northern NJ, USA
Posts: 151
Oh. I get it. Kinda. Just another way to confuse us.

Thanks for the info (except I thought I'm using the Korn shell...)
  #7 (permalink)  
Old 05-29-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 Bourne syntax is still valid in Korn, but not vice versa. In other words, Korn shell is backwards-compatible with the "classic" Bourne shell.

Just for completeness, [ test1 ] && [ test2 ] is valid Bourne syntax too.
Closed Thread

Bookmarks

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 09:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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