PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Shell function doesn't return correct value over 255?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Shell function doesn't return correct value over 255?

Réponse
 
LinkBack Outils de la discussion
Vieux 05/11/2007, 19h49   #1
mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Shell function doesn't return correct value over 255?

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

  Réponse avec citation
Vieux 05/11/2007, 20h18   #2
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Shell function doesn't return correct value over 255?

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.
  Réponse avec citation
Vieux 05/11/2007, 20h18   #3
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Shell function doesn't return correct value over 255?

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.
  Réponse avec citation
Vieux 05/11/2007, 20h29   #4
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Shell function doesn't return correct value over 255?

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.
  Réponse avec citation
Vieux 05/11/2007, 20h36   #5
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Shell function doesn't return correct value over 255?

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.
  Réponse avec citation
Vieux 05/11/2007, 21h00   #6
mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Shell function doesn't return correct value over 255?

Cyrus,

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

  Réponse avec citation
Vieux 05/11/2007, 22h41   #7
Wayne
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Shell function doesn't return correct value over 255?

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 s!

-Wayne
  Réponse avec citation
Vieux 05/11/2007, 23h19   #8
mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Shell function doesn't return correct value over 255?

Wayne,

Thank makes more sense. Thanks

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16494 seconds with 16 queries