From owner-freebsd-questions@FreeBSD.ORG Sun Jun 27 07:48:48 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 2AE24106564A for ; Sun, 27 Jun 2010 07:48:48 +0000 (UTC) (envelope-from swell.k@gmail.com) Received: from mail-vw0-f54.google.com (mail-vw0-f54.google.com [209.85.212.54]) by mx1.freebsd.org (Postfix) with ESMTP id CD0B08FC0C for ; Sun, 27 Jun 2010 07:48:47 +0000 (UTC) Received: by vws13 with SMTP id 13so5984115vws.13 for ; Sun, 27 Jun 2010 00:48:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:references :date:in-reply-to:message-id:user-agent:mime-version:content-type; bh=CJA/xx/Lj4wy3w/u4arPxIADB7XgrT6/+ZvOY0Sdhn0=; b=KoumW+t+wj1hReUoHbTskGfmlfntuWweWjrwrr+gbzEVCFRzDEtWCy9l+NMPB2i8H3 0kEr75TQqJJUYNaEuQnISiIVZZxcm9B4R4h0UGDlQur42gvLB8P8X8LW/r8vQJfMnJ4B Par3veqmNc+JweI7qjCzDfMhGN4Hz3YJGLNp0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; b=vvFL+yVJNsgJcq7SS6HcdDpPHaeNPm2Q03gb2mvkn8ptkv0W54mS5bFCXj5mbdUTXs oQDB3PrSPYOQT2St3pCT9plF77wDDZKd/LDf536/JhIY7A2UCFd9d3imMAJ/e213nrse ReFQgS/iZ8s0Bod4O3N1Zf2yE10AFFakEXSjY= Received: by 10.220.171.211 with SMTP id i19mr1842043vcz.252.1277623258585; Sun, 27 Jun 2010 00:20:58 -0700 (PDT) Received: from localhost ([80.62.217.18]) by mx.google.com with ESMTPS id w29sm2664445vcr.26.2010.06.27.00.20.45 (version=SSLv3 cipher=RC4-MD5); Sun, 27 Jun 2010 00:20:58 -0700 (PDT) From: Anonymous To: Giorgos Keramidas References: <4C22B3D7.6070102@comclark.com> <20100624013755.GA5009@gothschlampen.com> <20100624034434.7a6c2895@gumby.homeunix.com> <20100624031953.GA21766@gothschlampen.com> <87fx0dvwfd.fsf@kobe.laptop> Date: Sun, 27 Jun 2010 11:20:15 +0400 In-Reply-To: <87fx0dvwfd.fsf@kobe.laptop> (Giorgos Keramidas's message of "Thu, 24 Jun 2010 08:20:22 +0300") Message-ID: <86mxuhuekw.fsf@gmail.com> 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, Thomas Keusch 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: Sun, 27 Jun 2010 07:48:48 -0000 Giorgos Keramidas writes: > 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 $ printf %g $var 2>&- >&- ; echo $? 0 > > $ var=3a.1415926535897931 > $ python -c "$var + 0.0" >/dev/null 2>&1 ; echo $? > 1 $ printf %g $var 2>&- >&- ; echo $? 1 It also understands %e and %a -notation, e.g. 3.14e+2 and 0x1.3ap+8. $ python -c 0x1.3ap+8 2>&- >&- ; echo $? 1 $ printf %g 0x1.3ap+8 2>&- >&- ; echo $? 0 > > 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. Besides, printf(1) is also builtin in some shells which can reduce overhead of spawning process. IIRC, there is some support for builtin printf in our /bin/sh but it's disabled.