bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: expr exit status


From: salih k
Subject: Re: expr exit status
Date: Wed, 20 Jan 2010 20:07:18 +0800

On Wed, Jan 20, 2010 at 7:33 PM, Philip Rowlands <address@hidden> wrote:

> On Wed, 20 Jan 2010, salih k wrote:
>
> add_num=`expr $int_num + 1 1>/dev/null 2>&1`
>>
>>       if [ "$?" -ne "0" ]
>>
>> some case exit status is 1552 for expr even though the argument is integer
>>
>
> This cannot be true, as the exit status exposed through the $? shell
> variable is limited to the range 0-255. For example,
>
> $ sh -c 'exit 255'; echo $?
> 255
> $ sh -c 'exit 1552'; echo $?
> 16
>
> As Eric has (patiently) explained there are better ways in shell to test
> for an integer, so I'd suggest using those.
>
> For reference, this is what expr will do with various inputs:
>
> $ expr 5 + 1 >/dev/null; echo $?
> 0
>
> $ expr -1 + 1 >/dev/null; echo $?
> 1
>
> $ expr fish + 1 >/dev/null; echo $?
> expr: non-numeric argument
> 2
>
>
> Cheers,
> Phil
>

--------
Hi ,

Thanks,
Means if the exit status exceeding 255 we have to take the reminder of
modulus 256.

so if I f take the modulus 256 for all the exit status exceeding 255 the
result will be same as you said
$ sh -c 'exit 255'; echo $?
255
$ sh -c 'exit 1552'; echo $?
16

Here 51198 mod 256= 254

1552 mod 256=16



Now shall I explain again (sorry:))

 actually the issue is rare and never happened in unix .So am curious on
this.



Now my query is why these exit status (254 or 16) are coming during the
first run then it become correct value after rerun?and

*what does it mean by exit status 16 and 254?Whether this can do any thing
with expr or issue with *

*/dev/null etc?*



As pen man pages(sorry if it is repeating)

 b.info expr|more
    Exit status:

      0 if the expression is neither null nor 0,
      1 if the expression is null or 0,
      2 if the expression is syntactically invalid,
      3 if an error occurred.

there is nothing defined if >3.Thats why am asking *whether I can use if [
$? -eq 1 -o $? -eq 2 -o $? -eq 3 ] instead of*

*if [ $? -ne 0 ].my expr version:5.97*

**

*In case of HP unix it specifies that for expr*


*            1     Expression is null or zero.*

*            2     Invalid expression.*

*           >2     An error occurred while evaluating the expression.
Here the exit status for expr can be greater than 2 also.So there is a
meaning in if [ $? -ne 0 ].But in coreutils expr the man page says only 0 to
3.Can any one pls check and *



If i should not use if [ $? -eq 1 -o $? -eq 2 -o $? -eq 3 ] then i will go
ahead with case.But the current expr is a running codew so my manager need
explanation.thats why.If expr is not stable i have to change all the places
in script which uses expr.

If *this is the only way(ie expr is not stable) then i will go ahead with
case*



   case $int_num in( i belive i have to use *case "$int_num" *for more
safety)
      *[!0-9]*|'')

      echo nonumeric

      continue
     ;;
   esac

Here more testing effort is reqd thats why am sticking to expr.



Thanks,

Salih


reply via email to

[Prev in Thread] Current Thread [Next in Thread]