.sh check for numeric content

Aiza aiza21 at comclark.com
Thu Jun 24 04:52:49 UTC 2010


Aiza wrote:
> Thomas wrote:
>> On Thu, Jun 24, 2010 at 09:24:39AM +0800, Aiza wrote:
>>
>> Hello,
>>
>>> Receiving a variable from the command line that is suppose
>>> to contain numeric values.
>>>
>>> How do I code a test to verify the content is  numeric?
>>
>> http://www.google.com/search?q=shell+test+if+variable+numeric
>>
>> First link =>
>> http://www.unix.com/shell-programming-scripting/46276-check-variable-if-its-non-numeric.html 
>>
>>
>> Gosh, Google is full of answers these days..
>>
> 
> yea but none of them are for freebsd style .sh shell
> 
> 
> I'm, using
> 
> [ "${dup_times}" != [0-9] ] && exerr "value not numeric"
> 
> and get the errot messahe no mater what value is in dup_times.
> 
> What is wrong with this code?
> 
> 

Tried this suggestion from a reply and it worked.
Only valid numeric value is whole numbers.

if expr "${dup_times}" : "[0-9]*$"
then
    echo "value is numeric"
else
    echo "value is not numeric"
fi


But when I tried this format
[ expr "${dup_times}" : "[0-9]*$" ] || echo "value is not numeric"

I get the error message no mater what the value is.

What am I doing wrong?





More information about the freebsd-questions mailing list