LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 01-05-2009, 01:46 PM   #1
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Rep: Reputation: 15
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
 
Old 01-05-2009, 01:57 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
How about testing the return code for the "test" ([) function? eg:

if $?; then <something>; fi
 
Old 01-05-2009, 03:34 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Here is a dirty but effective way:
Code:
[ $var -lt 1 -o $var -gt 50 2> /dev/null ] && dothis_1 || dothis_2
 
Old 01-05-2009, 05:10 PM   #4
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
Thanks for the help.
 
  


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
perl: how to insert numerical digit immediately after regexp backreference variable? chadwick Programming 8 05-19-2008 12:49 PM
Test variable is an integer in shell judgex Programming 1 06-03-2006 09:12 PM
How to test if an env variable is set King of Men Linux - Newbie 5 11-03-2005 04:39 AM
test a variable to see if it contains information farmerjoe Programming 4 04-25-2005 02:34 PM
Test uninitialized variable AMMullan Programming 6 02-12-2004 10:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 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