Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 24 Jun 2010 09:20:36 +0700
From:      "Anh Ky Huynh" <xkyanh@gmail.com>
To:        Aiza <aiza21@comclark.com>
Cc:        "questions@freebsd.org" <questions@freebsd.org>
Subject:   Re: .sh  check for numeric content
Message-ID:  <20100624092036.6462fc1a@icy.localdomain>
In-Reply-To: <4C22B3D7.6070102@comclark.com>
References:  <4C22B3D7.6070102@comclark.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 24 Jun 2010 09:24:39 +0800
Aiza <aiza21@comclark.com> wrote:

> 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?

echo "$your_variable" | grep -E "^[0-9]+(\.[0-9]*)*[0-9]+$"

If $your_variable is numeric (123, or 123.123, etc), the return code should be 0. You can custom to script to support negative numbers.

-- 
Anh Ky Huynh



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