From owner-freebsd-stable@FreeBSD.ORG Fri Sep 21 17:34:10 2012 Return-Path: Delivered-To: stable@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id D0CA4106566C for ; Fri, 21 Sep 2012 17:34:10 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from mx1.stack.nl (unknown [IPv6:2001:610:1108:5012::107]) by mx1.freebsd.org (Postfix) with ESMTP id 5FC838FC12 for ; Fri, 21 Sep 2012 17:34:10 +0000 (UTC) Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131]) by mx1.stack.nl (Postfix) with ESMTP id 9F24712013A; Fri, 21 Sep 2012 19:34:06 +0200 (CEST) Received: by snail.stack.nl (Postfix, from userid 1677) id 756FA2847B; Fri, 21 Sep 2012 19:34:06 +0200 (CEST) Date: Fri, 21 Sep 2012 19:34:06 +0200 From: Jilles Tjoelker To: David Wolfskill Message-ID: <20120921173406.GC9070@stack.nl> References: <20120921170902.GC28959@albert.catwhisker.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20120921170902.GC28959@albert.catwhisker.org> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: stable@freebsd.org Subject: Re: /bin/sh arithmetic doesn't seem to like leading 0 now X-BeenThere: freebsd-stable@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Production branch of FreeBSD source code List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Sep 2012 17:34:10 -0000 On Fri, Sep 21, 2012 at 10:09:02AM -0700, David Wolfskill wrote: > I have a construct in a shell script that I had been using under > stable/8 (most recently, @r240259), but which throws an error under > stable/9 (at least as early as @r238602): > $ echo $(( ( $( date +%m ) - 1 ) / 3 + 1 )) > 3 > $ uname -r > 8.3-PRERELEASE > $ echo $(( ( $( date +%m ) - 1 ) / 3 + 1 )) > arithmetic expression: expecting ')': " ( 09 - 1 ) / 3 + 1 " > $ uname -r > 9.1-PRERELEASE > Trying bits & pieces of the above, I narrowed the issue down to: > $ echo $(( 09 + 0 )) > arithmetic expression: expecting EOF: " 09 + 0 " > while > $ echo $(( 9 + 0 )) > 9 > $ > is not a problem. > Is this intentional? Yes, it was changed with r216547, December 2010. This was done to avoid an inconsistency where constants starting with "0" and containing "8" or "9" were decimal, so something like $((018-017)) expanded to 3. There are indeed various cases where this inconsistency does not matter (because the numbers with leading zeroes do not exceed 10). > (I can work around it -- e.g., by using sed to strip leading 0 from the > month number (since strftime() doesn't appear to have a format that > provides the value in a form that lacks the leading zero for values < > 10). But I'd rather not do that if I don't need to.) You can use date +%-m although it is not in POSIX. With POSIX only, it is still possible to do it reasonably efficiently, for example $(( 1$(date +%m) - 100 )) or v=$(date +%m); v=${v#0}. -- Jilles Tjoelker