Shell function doesn"t return correct value over 255?

Shell function doesn"t return correct value over 255?

am 05.11.2007 20:49:17 von Mike

>From all material I collected a shell function returned value is kept
in $? without any limit, but I got some weird problems. Here is a
example:

--------------------------
#!/bin/sh
###!/bin/bash

add1() {
x=`expr $1 + 1`
#echo -e "x=$x"
return $x
}

a=1
add1 $a
echo -e "a=$a\t$?"

a=2
add1 $a
echo -e "a=$a\t$?"

a=254
add1 $a
echo -e "a=$a\t$?"

a=255
add1 $a
echo -e "a=$a\t$?"

a=511
add1 $a
echo -e "a=$a\t$?"

a=512
add1 $a
echo -e "a=$a\t$?"

a=1026
add1 $a
echo -e "a=$a\t$?"


--------------------------
I got output:
a=1 2
a=2 3
a=254 255
a=255 0
a=511 0
a=512 1
a=1026 3


Could someone please explain how the $? treats with the return value?
Is it possible that $? can only keep 1 byte?

Many thanks in advance.

Re: Shell function doesn"t return correct value over 255?

am 05.11.2007 21:18:06 von Cyrus Kriticos

mike wrote:
>>From all material I collected a shell function returned value is kept
> in $? without any limit, but I got some weird problems.

$? is limitied to 0-255 in bash.

Your script output with bash:

a=1 2
a=2 3
a=254 255
a=255 0
a=511 0
a=512 1
a=1026 3

Your script output with dash:

-e a=1 2
-e a=2 3
-e a=254 255
-e a=255 256
-e a=511 512
-e a=512 513
-e a=1026 1027

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Shell function doesn"t return correct value over 255?

am 05.11.2007 21:18:51 von Cyrus Kriticos

mike wrote:
>>From all material I collected a shell function returned value is kept
> in $? without any limit, but I got some weird problems.

$? is limited to 0-255 in bash.

Your script output with bash:

a=1 2
a=2 3
a=254 255
a=255 0
a=511 0
a=512 1
a=1026 3

Your script output with dash:

-e a=1 2
-e a=2 3
-e a=254 255
-e a=255 256
-e a=511 512
-e a=512 513
-e a=1026 1027

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Shell function doesn"t return correct value over 255?

am 05.11.2007 21:29:52 von Cyrus Kriticos

mike wrote:
>>From all material I collected a shell function returned value is kept
> in $? without any limit, but I got some weird problems. Here is a
> example:
>
> --------------------------
> #!/bin/sh
> ###!/bin/bash
>
> add1() {
> x=`expr $1 + 1`
> #echo -e "x=$x"
> return $x
> }
>
> [...]
>
> a=1026
> add1 $a
> echo -e "a=$a\t$?"

ugly workaround with subshell:

--- cut here ---
#!/bin/bash

add1() {
x=`expr $1 + 1`
#echo -e "x=$x"
echo $x # changed
}

[...]

a=1026
ret=$(add1 $a)
echo -e "a=$a\t$ret" # changed
--- cut here ---

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Shell function doesn"t return correct value over 255?

am 05.11.2007 21:36:05 von Cyrus Kriticos

mike wrote:
>>From all material I collected a shell function returned value is kept
> in $? without any limit, but I got some weird problems. Here is a
> example:
>
> --------------------------
> #!/bin/sh
> ###!/bin/bash
>
> add1() {
> x=`expr $1 + 1`
> #echo -e "x=$x"
> return $x
> }
>
> [...]
>
> a=1026
> add1 $a
> echo -e "a=$a\t$?"

ugly workaround with subshell:

--- cut here ---
#!/bin/bash

add1() {
x=`expr $1 + 1`
#echo -e "x=$x"
echo $x # changed
}

[...]

a=1026
ret=$(add1 $a) # changed
echo -e "a=$a\t$ret" # changed
--- cut here ---

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: Shell function doesn"t return correct value over 255?

am 05.11.2007 22:00:11 von Mike

Cyrus,

Never tried dash.
The last method is great! Many thanks!!

Re: Shell function doesn"t return correct value over 255?

am 05.11.2007 23:41:30 von wayne

mike wrote:
>>From all material I collected a shell function returned value is kept
> in $? without any limit, but I got some weird problems. Here is a
> example:
>
> --------------------------
> #!/bin/sh
> ###!/bin/bash
>
> add1() {
> x=`expr $1 + 1`
> #echo -e "x=$x"
> return $x
> }
>
> a=1
> add1 $a
> echo -e "a=$a\t$?"
>
> a=2
> add1 $a
> echo -e "a=$a\t$?"
>
> a=254
> add1 $a
> echo -e "a=$a\t$?"
>
> a=255
> add1 $a
> echo -e "a=$a\t$?"
>
> a=511
> add1 $a
> echo -e "a=$a\t$?"
>
> a=512
> add1 $a
> echo -e "a=$a\t$?"
>
> a=1026
> add1 $a
> echo -e "a=$a\t$?"
>
>
> --------------------------
> I got output:
> a=1 2
> a=2 3
> a=254 255
> a=255 0
> a=511 0
> a=512 1
> a=1026 3
>
>
> Could someone please explain how the $? treats with the return value?
> Is it possible that $? can only keep 1 byte?
>
> Many thanks in advance.
>

That's not a bug. POSIX says for the "return shell built-in):

EXIT STATUS
The value of the special parameter '?' shall be set to n, an unsigned
decimal integer, or to the exit status of the last command executed
if n is not specified. If the value of n is greater than 255, the
results are undefined.

Many OSes only allow a one byte value for an exit status. On some
(older) systems the byte is a signed int and thus the return value
is in -128 to +127. (So you are only safe on all systems if you use
return values in 0 to +127.)

The exit status is only meant to be used to indicate success or
failure of a script, program, or shell function. The only time
you should use this return value as data is when you are sure
the value is small enough (e.g., when writing card games in
shell, each card has a number from 1 to 52, so that works).
Otherwise, keep in mind the return in shell is NOT the same as
the return statement in a C or similar programs.

You have two choices to return data from a shell function to the
calling script: Send the result to stdout and invoke your
functions as:

result="$(my_func)"

Or, use a global variable to transfer the result:

export RESULT

my_func()
{ ans=$(expr $1 + 1)
RESULT="ans"
}

....
my_func 300
echo RESULT is $RESULT

This issue is a pain in the a..., er, neck, for many.
Maybe the 2008 version of the SUS will address this.

Hope this helps!

-Wayne

Re: Shell function doesn"t return correct value over 255?

am 06.11.2007 00:19:45 von Mike

Wayne,

Thank makes more sense. Thanks