Date: Thu, 24 Jun 2010 10:34:07 -0700 From: Carl Johnson <carlj@peak.org> To: freebsd-questions@freebsd.org Subject: Re: .sh check for numeric content Message-ID: <87d3vgmj1s.fsf@cjlinux.localnet> In-Reply-To: <87hbksmk6y.fsf@cjlinux.localnet> (Carl Johnson's message of "Thu\, 24 Jun 2010 10\:09\:25 -0700") References: <4C22B3D7.6070102@comclark.com> <20100624033257.2D074BEA6@kev.msw.wpafb.af.mil> <87lja4mlme.fsf@cjlinux.localnet> <87hbksmk6y.fsf@cjlinux.localnet>
next in thread | previous in thread | raw e-mail | index | archive | help
Carl Johnson <carlj@peak.org> writes: > Carl Johnson <carlj@peak.org> writes: > >> vogelke+unix@pobox.com (Karl Vogel) writes: >> >>>>> On Thu, 24 Jun 2010 09:24:39 +0800, >>>>> Aiza <aiza21@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. > > I just realized that I had a stupid mistake there and should have > used: > if expr "$arg" : "[0-9]*\.[0-9]*$" > /dev/null And of course that was another stupid mistake that I didn't test properly. I really wanted 0 or 1 decimal points, so I wanted '\.\?', except that FreeBSD expr doesn't recognize '\?'. I finally ended up with the following which seems to work as *I* expected it to work: if expr "$arg" : "[1-9]*\.\{0,1\}[0-9]*$" > /dev/null -- Carl Johnson carlj@peak.org
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?87d3vgmj1s.fsf>