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
Ksh How to test if variable is numeric?? developncode UNIX for Dummies Questions & Answers 8 03-19-2009 06:42 PM
Test variable javeed7 Shell Programming and Scripting 2 04-19-2008 07:59 PM
Test whether absolute path in variable dkieran Shell Programming and Scripting 2 05-21-2007 05:14 PM
test Null variable kykyboss Shell Programming and Scripting 1 11-24-2006 12:02 PM
How to test if a variable is in the right format dbrundrett Shell Programming and Scripting 2 07-05-2004 07:05 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 01-05-2009
tostay2003 tostay2003 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
variable numerical test

Hi

I have a variable which should be any number between 1 and 50.

It could also be any string/empty string.

I have a code written below. The point is when the variable contains string.
I don't want the code below to error out. Instead fall in the else bucket.

Code:
[ $var -lt 1 -o $var -gt 50 ] && dothis_1 || dothis_2
  #2 (permalink)  
Old 01-06-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by tostay2003 View Post
Hi

I have a variable which should be any number between 1 and 50.

It could also be any string/empty string.

I have a code written below. The point is when the variable contains string.
I don't want the code below to error out. Instead fall in the else bucket.

You don't have an "else bucket"; dothis_2 will be executed if either the numerical test or dothis_1 fails.
Quote:
Code:
[ $var -lt 1 -o $var -gt 50 ] && dothis_1 || dothis_2

Code:
if case $var in
  *[!0-9]* | "" ) false ;;
  *) true ;;
esac && [ $var -ge 1 ] && [ $var -le 50 ]
then
  echo dothis_1
else
  echo dothis_2
fi
Sponsored Links
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 04:27 PM.


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