Re: expr / (( )) different behavior

2017-08-11 Thread Ingo Schwarze
Hi,

Alessandro DE LAURENZIS wrote on Fri, Aug 11, 2017 at 01:14:48PM +0200:
> On Fri 11/08/2017 13:07, Janne Johansson wrote:

>> 0 is parsed as octal in places, so 09 would be bogus if octal.

> Thanks for the clarification; does that mean expr(1) can treat 10-base 
> numbers only? No info in man page on this matter...

POSIX restricts expr(1) to handle decimal integers only and has some
complicated wording about which strings will be treated as integers
and which won't.  I consider that too cumbersome to explain it in
the manual, in particular since it will rarely matter in practice.

But being specific about the number base adds precision and clarity
without harming readability.

OK?
  Ingo


Index: expr.1
===
RCS file: /cvs/src/bin/expr/expr.1,v
retrieving revision 1.23
diff -u -p -r1.23 expr.1
--- expr.1  16 Jan 2015 15:30:10 -  1.23
+++ expr.1  11 Aug 2017 16:45:29 -
@@ -39,16 +39,17 @@ Returns the evaluation of
 if neither expression evaluates to an empty string or zero;
 otherwise, returns zero.
 .It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
-Returns the results of integer comparison if both arguments are integers;
-otherwise, returns the results of string comparison using the locale-specific
-collation sequence.
+Returns the results of integer comparison if both arguments are
+decimal integers; otherwise, returns the results of string comparison
+using the locale-specific collation sequence.
 The result of each comparison is 1 if the specified relation is true,
 or 0 if the relation is false.
 .It Ar expr1 Li "{+, -}" Ar expr2
-Returns the results of addition or subtraction of integer-valued arguments.
+Returns the results of addition or subtraction of decimal integer-valued
+arguments.
 .It Ar expr1 Li "{*, /, %}" Ar expr2
 Returns the results of multiplication, integer division, or remainder of
-integer-valued arguments.
+decimal integer-valued arguments.
 .It Ar expr1 Li \&: Ar expr2
 The
 .Ql \&:



Re: expr / (( )) different behavior

2017-08-11 Thread Janne Johansson
2017-08-11 13:14 GMT+02:00 Alessandro DE LAURENZIS :

> Hi Janne,
> On Fri 11/08/2017 13:07, Janne Johansson wrote:
>
>> 0 is parsed as octal in places, so 09 would be bogus if octal.
>>
> [...]
>
> Thanks for the clarification; does that mean expr(1) can treat 10-base
> numbers only? No info in man page on this matter...
>

Well, I think the default would be for simple math stuff to only use base
10.
Then stuff like $(()) should perhaps say it accepts hex,octal or whatever
outside of that.
$ echo $(( 09 + 1))
ksh:  09 + 1: bad number `09'
$ echo $(( 08 + 1))
ksh:  08 + 1: bad number `08'
$ echo $(( 07 + 1))
8

I have used the "skip leading zeros" at times with expr in order to handle
possibly empty environment vars with stuff like:
sleep $(expr 0$MIGHT_HAVE_VALUE + 1) so that an unset env-var will not make
expr treat it like $(expr + 1) which would
be an error.

-- 
May the most significant bit of your life be positive.


Re: expr / (( )) different behavior

2017-08-11 Thread Alessandro DE LAURENZIS

Hi Janne,

On Fri 11/08/2017 13:07, Janne Johansson wrote:

0 is parsed as octal in places, so 09 would be bogus if octal.

[...]

Thanks for the clarification; does that mean expr(1) can treat 10-base 
numbers only? No info in man page on this matter...


--
Alessandro DE LAURENZIS
[mailto:jus...@atlantide.t28.net]
LinkedIn: http://it.linkedin.com/in/delaurenzis



Re: expr / (( )) different behavior

2017-08-11 Thread Janne Johansson
0 is parsed as octal in places, so 09 would be bogus if octal.


2017-08-11 12:56 GMT+02:00 Alessandro DE LAURENZIS :

> Dear misc@ readers,
>
> I was doing a little exercise with integer arithmetics and noticed the
> following:
>
> [snip]
> $ echo $(expr -09 % 3)
> 0
> [snip]
>
> [snip]
> $ echo $((-09 % 3))
> sh: -09 % 3: bad number `09'
> [snip]
>
> bash seems to behave same way; just wondering if this inconsistency is
> expected...
>
> --
> Alessandro DE LAURENZIS
> [mailto:jus...@atlantide.t28.net]
> LinkedIn: http://it.linkedin.com/in/delaurenzis
>
>


-- 
May the most significant bit of your life be positive.