Re: /bin/sh arithmetic doesn't seem to like leading 0 now

2012-09-23 Thread Jilles Tjoelker
On Fri, Sep 21, 2012 at 10:26:37PM +, David O'Brien wrote:
 On Fri, Sep 21, 2012 at 07:34:06PM +0200, Jilles Tjoelker wrote:
  On Fri, Sep 21, 2012 at 10:09:02AM -0700, David Wolfskill wrote:
   $ echo $(( ( $( date +%m ) - 1 ) / 3 + 1 ))
   arithmetic expression: expecting ')':  ( 09 - 1 ) / 3 + 1 
 ...
  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.

 Jilles,
 Would it be possible to improve on the error message?
 If David had been given the Bash error message, I suspect he would have
 figured out the issue right away.

It would certainly be possible to add a new error message, but from the
embedded point of view the extra code size may not be worth it (also
considering that error messages can be enhanced in many other places as
well).

-- 
Jilles Tjoelker
___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to freebsd-stable-unsubscr...@freebsd.org


Re: /bin/sh arithmetic doesn't seem to like leading 0 now

2012-09-21 Thread Brandon Allbery
On Fri, Sep 21, 2012 at 1:09 PM, David Wolfskill da...@catwhisker.orgwrote:

 $ 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  allber...@gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to freebsd-stable-unsubscr...@freebsd.org


Re: /bin/sh arithmetic doesn't seem to like leading 0 now

2012-09-21 Thread David Wolfskill
On Fri, Sep 21, 2012 at 01:20:07PM -0400, Brandon Allbery wrote:
 On Fri, Sep 21, 2012 at 1:09 PM, David Wolfskill da...@catwhisker.orgwrote:
 
  $ echo $(( 09 + 0 ))
 
 
 Unable to get to fbsd box now but suspicious mind wants to know what
 happens with 07 in place of 09.

As (I) expected, it's handled Just fine:

$ uname -r
9.1-PRERELEASE
$ echo $(( 07 + 0 ))
7
$ 

So yeah, this seems to be something involving treating leading 0 as
indicating octal (and seriously disliking 9 in such a context).

Peace,
david
-- 
David H. Wolfskill  da...@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.


pgptkIFmc1Vbm.pgp
Description: PGP signature


Re: /bin/sh arithmetic doesn't seem to like leading 0 now

2012-09-21 Thread Jilles Tjoelker
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
___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to freebsd-stable-unsubscr...@freebsd.org


Re: /bin/sh arithmetic doesn't seem to like leading 0 now

2012-09-21 Thread David Wolfskill
On Fri, Sep 21, 2012 at 07:34:06PM +0200, Jilles Tjoelker wrote:
 ...
  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.

Ah; OK.

 There are indeed various cases where this inconsistency does not matter
 (because the numbers with leading zeroes do not exceed 10).

Sure.  Especially when working with month numbers. :-}

  (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.

Ah -- I see that now (%-*   GNU libc extension)

 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}.
 ...

Thanks.

Peace,
david
-- 
David H. Wolfskill  da...@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.


pgpASE76ajrnq.pgp
Description: PGP signature


Re: /bin/sh arithmetic doesn't seem to like leading 0 now

2012-09-21 Thread Daryl Richards

On 21/09/2012 1:20 PM, Brandon Allbery wrote:

On Fri, Sep 21, 2012 at 1:09 PM, David Wolfskill da...@catwhisker.orgwrote:


$ echo $(( 09 + 0 ))


Unable to get to fbsd box now but suspicious mind wants to know what
happens with 07 in place of 09.



Interestingly enough, bash gives a proper explanation:

bash$ echo $(( 09 + 0 ))
-bash: 09: value too great for base (error token is 09)
bash$ echo $(( 07 + 0 ))
7

--
Daryl Richards
Isle Technical Services Inc
(519) 573-3399

___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to freebsd-stable-unsubscr...@freebsd.org


Re: /bin/sh arithmetic doesn't seem to like leading 0 now

2012-09-21 Thread David O'Brien
On Fri, Sep 21, 2012 at 07:34:06PM +0200, Jilles Tjoelker wrote:
 On Fri, Sep 21, 2012 at 10:09:02AM -0700, David Wolfskill wrote:
  $ echo $(( ( $( date +%m ) - 1 ) / 3 + 1 ))
  arithmetic expression: expecting ')':  ( 09 - 1 ) / 3 + 1 
...
 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.

Jilles,
Would it be possible to improve on the error message?
If David had been given the Bash error message, I suspect he would have
figured out the issue right away.

-- 
-- David(obr...@freebsd.org)
___
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to freebsd-stable-unsubscr...@freebsd.org