Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 24 Jun 2010 08:20:22 +0300
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
To:        Thomas Keusch <fwd@bsd-solutions-duesseldorf.de>
Cc:        RW <rwmaillists@googlemail.com>, freebsd-questions@freebsd.org
Subject:   Re: .sh  check for numeric content
Message-ID:  <87fx0dvwfd.fsf@kobe.laptop>
In-Reply-To: <20100624031953.GA21766@gothschlampen.com> (Thomas Keusch's message of "Thu, 24 Jun 2010 05:19:53 %2B0200")
References:  <4C22B3D7.6070102@comclark.com> <20100624013755.GA5009@gothschlampen.com> <20100624034434.7a6c2895@gumby.homeunix.com> <20100624031953.GA21766@gothschlampen.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 24 Jun 2010 05:19:53 +0200, Thomas Keusch <fwd@bsd-solutions-duesseldorf.de> wrote:
> tk@eternity:~$ b=5
> tk@eternity:~$ case "$b" in
>> [0-9] )
>>         echo numeric
>>         ;;
>> * )
>>         echo alpha
>>         ;;
>> esac
> numeric
> tk@eternity:~$
>
> Works for me.

Depending on what "numeric" means, this may be ok.  For other numeric
values (e.g. floating point numbers) There are simple, fast and correct
ways to check but you have to escape from the shell, e.g.:

    $ var=3.1415926535897931
    $ python -c "$var + 0.0" >/dev/null 2>&1 ; echo $?
    0

    $ var=3a.1415926535897931
    $ python -c "$var + 0.0" >/dev/null 2>&1 ; echo $?
    1

The overhead of spawning a full-blown language interpreter like Perl or
Python may be acceptable if you have to check "a few" values.  Then it
may be overkill if you want to check a million values.  It's really up
to you, as a programmer, to pick the right method.




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