.sh check for numeric content

Carl Johnson carlj at peak.org
Thu Jun 24 16:38:36 UTC 2010


vogelke+unix at pobox.com (Karl Vogel) writes:

>>> On Thu, 24 Jun 2010 09:24:39 +0800, 
>>> Aiza <aiza21 at comclark.com> said:
>
> A> Receiving a variable from the command line that is suppose to contain
> A> numeric values.  How do I code a test to verify the content is numeric?
>
>    The script below will work with the Bourne or Korn shell.
>    Results for "0 1 12 1234 .12 1.234 12.3 1a a1":
>
>      0 is numeric
>      1 is numeric
>      12 is numeric
>      1234 is numeric
>      .12 is numeric
>      1.234 is numeric
>      12.3 is numeric
>      1a is NOT numeric
>      a1 is NOT numeric

You might want to try testing "123..45".
I tried changing:
>    if expr "$arg" : "[0-9]*[\.0-9]*$" > /dev/null
to:
    if expr "$arg" : "[0-9]*\.*[0-9]*$" > /dev/null
but it still claims that it is numeric, so *I* must be missing
something.

-- 
Carl Johnson		carlj at peak.org



More information about the freebsd-questions mailing list