From owner-freebsd-questions@FreeBSD.ORG Thu Jun 24 05:21: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 4303F106564A for ; Thu, 24 Jun 2010 05:21:01 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id ABB198FC0C for ; Thu, 24 Jun 2010 05:20:57 +0000 (UTC) X-Spam-Status: No X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-0.2, required 5, autolearn=not spam, ALL_TRUSTED -1.00, BAYES_50 0.80) X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-ID: o5O5KTw9021576 Received: from kobe.laptop (178.128.23.44.dsl.dyn.forthnet.gr [178.128.23.44]) (authenticated bits=128) by igloo.linux.gr (8.14.3/8.14.3/Debian-9.1) with ESMTP id o5O5KTw9021576 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Thu, 24 Jun 2010 08:20:35 +0300 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.4/8.14.4) with ESMTP id o5O5KNZW009460 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 24 Jun 2010 08:20:23 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.4/8.14.4/Submit) id o5O5KM8H009457; Thu, 24 Jun 2010 08:20:22 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) From: Giorgos Keramidas To: Thomas Keusch References: <4C22B3D7.6070102@comclark.com> <20100624013755.GA5009@gothschlampen.com> <20100624034434.7a6c2895@gumby.homeunix.com> <20100624031953.GA21766@gothschlampen.com> Date: Thu, 24 Jun 2010 08:20:22 +0300 In-Reply-To: <20100624031953.GA21766@gothschlampen.com> (Thomas Keusch's message of "Thu, 24 Jun 2010 05:19:53 +0200") Message-ID: <87fx0dvwfd.fsf@kobe.laptop> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: RW , freebsd-questions@freebsd.org Subject: Re: .sh check for numeric content 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 05:21:01 -0000 On Thu, 24 Jun 2010 05:19:53 +0200, Thomas Keusch 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.