Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 24 Jun 2010 05:19:53 +0200
From:      Thomas Keusch <fwd@bsd-solutions-duesseldorf.de>
To:        RW <rwmaillists@googlemail.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: .sh  check for numeric content
Message-ID:  <20100624031953.GA21766@gothschlampen.com>
In-Reply-To: <20100624034434.7a6c2895@gumby.homeunix.com>
References:  <4C22B3D7.6070102@comclark.com> <20100624013755.GA5009@gothschlampen.com> <20100624034434.7a6c2895@gumby.homeunix.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Jun 24, 2010 at 03:44:34AM +0100, RW wrote:

Hello,

> On Thu, 24 Jun 2010 03:37:55 +0200
> Thomas <fwd@gothschlampen.com> 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..
> 
> I'd suggest looking a bit further down the list since the quoted first
> link is patently wrong.

tk@eternity:~$ b=5
tk@eternity:~$ case "$b" in 
> [0-9] ) 
>         echo numeric 
>         ;;
> * ) 
>         echo alpha 
>         ;;
> esac
numeric
tk@eternity:~$

Works for me.

Another solution would be like this:

if echo "$b" | egrep -q '^[0-9]+$'; then

and eventual variants of it.


Regards
Thomas



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20100624031953.GA21766>