From owner-freebsd-questions@FreeBSD.ORG Thu Jun 24 18:31:01 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B84DE1065674 for ; Thu, 24 Jun 2010 18:31:01 +0000 (UTC) (envelope-from parv@pair.com) Received: from hrndva-omtalb.mail.rr.com (hrndva-omtalb.mail.rr.com [71.74.56.124]) by mx1.freebsd.org (Postfix) with ESMTP id 6E2C58FC18 for ; Thu, 24 Jun 2010 18:31:01 +0000 (UTC) X-Authority-Analysis: v=1.1 cv=fhltGCNqn7I0vuW4QMEj3LCOhQQl14bnXIhcDW1W0Bg= c=1 sm=0 a=RWmFJ1rmKfAA:10 a=UBIxAjGgU1YA:10 a=kj9zAlcOel0A:10 a=64iin7ZDX+lUS8OB6KzQoA==:17 a=U9OQpdHBAAAA:8 a=ybZZDoGAAAAA:8 a=3__i-afVAAAA:8 a=8R6w6CTjIbAZaoM_jhwA:9 a=c5T5GmNZp82TPUEiBudZmM6EfkEA:4 a=CjuIK1q_8ugA:10 a=cUBdl-qfu3MA:10 a=qIVjreYYsbEA:10 a=YC5Rd9BSHxcA:10 a=eUwidNU-FBG3U93j:21 a=SqtkuudyxkkbMT3A:21 a=64iin7ZDX+lUS8OB6KzQoA==:117 X-Cloudmark-Score: 0 X-Originating-IP: 98.150.184.250 Received: from [98.150.184.250] ([98.150.184.250:49630] helo=localhost.hawaii.res.rr.com) by hrndva-oedge02.mail.rr.com (envelope-from ) (ecelerity 2.2.2.39 r()) with ESMTP id B8/61-05913-364A32C4; Thu, 24 Jun 2010 18:31:00 +0000 Received: from holstein.holy.cow (parv [127.0.0.2]) by localhost.hawaii.res.rr.com (Postfix) with ESMTP id F0E5E5CB0; Thu, 24 Jun 2010 08:34:08 -1000 (HST) Received: (from parv@localhost) by holstein.holy.cow (8.14.3/8.14.3/Submit) id o5OIY8SS050310; Thu, 24 Jun 2010 08:34:08 -1000 (HST) (envelope-from parv@pair.com) X-Authentication-Warning: holstein.holy.cow: parv set sender to parv@pair.com using -f Date: Thu, 24 Jun 2010 08:34:07 -1000 From: parv@pair.com To: Carl Johnson Message-ID: <20100624183407.GA49923@holstein.holy.cow> Mail-Followup-To: Carl Johnson , freebsd-questions@freebsd.org References: <4C22B3D7.6070102@comclark.com> <20100624033257.2D074BEA6@kev.msw.wpafb.af.mil> <87lja4mlme.fsf@cjlinux.localnet> <87hbksmk6y.fsf@cjlinux.localnet> <87d3vgmj1s.fsf@cjlinux.localnet> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87d3vgmj1s.fsf@cjlinux.localnet> Cc: freebsd-questions@freebsd.org Subject: Re: check for numeric content in a shell script (FreeBSD sh) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Jun 2010 18:31:01 -0000 in message <87d3vgmj1s.fsf@cjlinux.localnet>, wrote Carl Johnson thusly... > > Carl Johnson writes: > > > Carl Johnson writes: > > > >> vogelke+unix@pobox.com (Karl Vogel) writes: > >> > >>>>> On Thu, 24 Jun 2010 09:24:39 +0800, > >>>>> Aiza said: > >>> > >>> A> Receiving a variable from the command line that is suppose > >>> A> to contain > >>> A> numeric values. How do I code a test to verify the content > >>> A> 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 That regex considers "." a number but not "0.9" (this one seems to be due to typo) nor a negative number. I would personally to use egrep or awk (printf "%s" "${arg}" | egrep "${regex}" [0]) instead of expr. - parv [0] Compact regex .... # Matches a number, either positive (without '+' sign) or # negative, which is either a whole number; or a real number # ending with decimal point, or a real number with or without # leading digits before the decimal point. ^ -? ( [0-9] [.]? [0-9]* | [0-9]? [.] [0-9]+ ) $ ...by removing whitespace before use. --