David Wolfskill
2012-Sep-21 17:09 UTC
/bin/sh arithmetic doesn't seem to like leading 0 now
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? (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.) Thanks! Peace, david -- David H. Wolfskill david@catwhisker.org Depriving a girl or boy of an opportunity for education is evil. See http://www.catwhisker.org/~david/publickey.gpg for my public key. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 196 bytes Desc: not available Url : http://lists.freebsd.org/pipermail/freebsd-stable/attachments/20120921/1b5e53e5/attachment.pgp
Brandon Allbery
2012-Sep-21 17:20 UTC
/bin/sh arithmetic doesn't seem to like leading 0 now
On Fri, Sep 21, 2012 at 1:09 PM, David Wolfskill <david@catwhisker.org>wrote:> $ echo $(( 09 + 0 )) >Unable to get to fbsd box now but suspicious mind wants to know what happens with 07 in place of 09. -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms
Jilles Tjoelker
2012-Sep-21 17:34 UTC
/bin/sh arithmetic doesn't seem to like leading 0 now
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