Home » Infrastructure » Unix » Ceck if the variable lies in a particular range
Ceck if the variable lies in a particular range [message #231222] Mon, 16 April 2007 01:22 Go to next message
lalit.thawani
Messages: 11
Registered: April 2007
Junior Member
Hi All..

I have a variable "a" with a numeral value.
How do i check if it lies between say 1 to 9 or not in a Unix script

Thanks in Advance
Re: Ceck if the variable lies in a particular range [message #231271 is a reply to message #231222] Mon, 16 April 2007 02:58 Go to previous message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
I assume you are talking about shell scripts.

I don't believe shell supports decimal values - only integers.

You can use typeset to enforce the variable is a number.
typeset -i a


Now that you know it is an integer, you can perform numeric comparisons.
if [ $a -ge 1 ] && [ $a -le 9 ] then
   do_something
fi


Alternatively, you can pipe the variable into and awk or a perl script. I try to avoid this as it launches a whole new process. It's very inefficient inside a loop.


Ross Leishman
Previous Topic: How to save a CSV (having non-English characters) file as XLS file
Next Topic: How to write a shell script to exec a proc with in and out params and also it has default values
Goto Forum:
  


Current Time: Thu Mar 28 21:07:37 CDT 2024