From owner-freebsd-questions@FreeBSD.ORG Thu Jun 24 03:46:58 2010 Return-Path: Delivered-To: questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 890E1106566B for ; Thu, 24 Jun 2010 03:46:58 +0000 (UTC) (envelope-from vogelke@hcst.net) Received: from beta.hcst.com (beta.hcst.com [192.52.183.241]) by mx1.freebsd.org (Postfix) with ESMTP id 522598FC14 for ; Thu, 24 Jun 2010 03:46:58 +0000 (UTC) Received: from beta.hcst.com (localhost [127.0.0.1]) by beta.hcst.com (8.14.3/8.14.3/Debian-5+lenny1) with ESMTP id o5O3YQeU012665 for ; Wed, 23 Jun 2010 23:34:26 -0400 Received: (from vogelke@localhost) by beta.hcst.com (8.14.3/8.14.3/Submit) id o5O3YQsd012664; Wed, 23 Jun 2010 23:34:26 -0400 Received: by kev.msw.wpafb.af.mil (Postfix, from userid 32768) id 2D074BEA6; Wed, 23 Jun 2010 23:32:57 -0400 (EDT) To: questions@freebsd.org Organization: Array Infotech X-Disclaimer: I don't speak for the USAF or Array Infotech. X-GPG-ID: 1024D/711752A0 2006-06-27 Karl Vogel X-GPG-Fingerprint: 56EB 6DBF 4224 C953 F417 CC99 4C7C 7D46 7117 52A0 References: <4C22B3D7.6070102@comclark.com> Message-Id: <20100624033257.2D074BEA6@kev.msw.wpafb.af.mil> Date: Wed, 23 Jun 2010 23:32:57 -0400 (EDT) From: vogelke+unix@pobox.com (Karl Vogel) Cc: Subject: Re: .sh check for numeric content X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: vogelke+unix@pobox.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Jun 2010 03:46:58 -0000 >> On Thu, 24 Jun 2010 09:24:39 +0800, >> Aiza 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 -- Karl Vogel I don't speak for the USAF or my company I place economy among the first and most important virtues, and public debt as the greatest of dangers to be feared. To preserve our independence, we must not let our rulers load us with perpetual debt. --Thomas Jefferson --------------------------------------------------------------------------- #!/bin/sh # Test an argument to see if it's numeric. Handles decimals, but # a minus sign in the regex will throw an error: "expr: illegal option". PATH=/bin:/usr/bin:/usr/local/bin export PATH case "$#" in 0) echo need an argument. ; exit 1 ;; *) ;; esac for arg do if expr "$arg" : "[0-9]*[\.0-9]*$" > /dev/null then echo "$arg is numeric" else echo "$arg is NOT numeric" fi done exit 0