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 > return values from a function call
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

return values from a function call

Réponse
 
LinkBack Outils de la discussion
Vieux 28/10/2006, 02h53   #1
paintedjazz@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut return values from a function call

If I use the result of a function call in a condtional statement,
is it possible to return one of three possible values or is it just
true or false or a 0 or 1 status?

I want to test if a number is greater than, equal to or less than
another number.
Thus, the function should be something like:

if myfunction $num $another_num # return value is probably only 0 or
1
then
echo $num is greater than
else
echo $num is less than
fi

But I need to know if $num and $another_num are equal too.

Can I just have the function return a value (other than the status)
that could be tested?
How could I do that? Thx for your .

  Réponse avec citation
Vieux 28/10/2006, 03h08   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: return values from a function call

On 2006-10-28, paintedjazz@gmail.com wrote:
> If I use the result of a function call in a condtional statement,
> is it possible to return one of three possible values or is it just
> true or false or a 0 or 1 status?
>
> I want to test if a number is greater than, equal to or less than
> another number.
> Thus, the function should be something like:
>
> if myfunction $num $another_num # return value is probably only 0 or
> 1
> then
> echo $num is greater than
> else
> echo $num is less than
> fi
>
> But I need to know if $num and $another_num are equal too.
>
> Can I just have the function return a value (other than the status)
> that could be tested?
> How could I do that? Thx for your .


A function can return any value from 0 to 255.

myfunction $num $another_num
case $? in
0) echo success; : ... ;;
1) echo returned 1; : .... ;;
....
255) : .... ;;
esac


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 28/10/2006, 03h18   #3
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: return values from a function call

paintedjazz@gmail.com wrote:
> If I use the result of a function call in a condtional statement,
> is it possible to return one of three possible values or is it just
> true or false or a 0 or 1 status?
>
> I want to test if a number is greater than, equal to or less than
> another number.
> Thus, the function should be something like:
>
> if myfunction $num $another_num # return value is probably only 0 or
> 1
> then
> echo $num is greater than
> else
> echo $num is less than
> fi
>
> But I need to know if $num and $another_num are equal too.
>
> Can I just have the function return a value (other than the status)
> that could be tested?
> How could I do that? Thx for your .
>


You could of course return values of 0, 1, and 2 from the function,
then test the return code through variable $? in a case statement.

fn() { ...
return $anyresult
}

case $? in
...
esac

But the intention of the exit code is rather to indicate success
(code 0) or failure (code >0), and conceptually the shell functions
are more like commands than like mathematical functions. Values from
functions are usually returned through the "print interface"...

fn() { ...
print $anyresult
}

result=$( fn arg1 arg2 )
if [ $result -eq ... ]

Or use a case statement...

case $( fn arg1 arg2 ) in
...
esac


Janis
  Réponse avec citation
Vieux 28/10/2006, 04h07   #4
Dave Kelly
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: return values from a function call

paintedjazz@gmail.com wrote:
> If I use the result of a function call in a condtional statement,
> is it possible to return one of three possible values or is it just
> true or false or a 0 or 1 status?
>
> I want to test if a number is greater than, equal to or less than
> another number.
> Thus, the function should be something like:
>
> if myfunction $num $another_num # return value is probably only 0 or
> 1
> then
> echo $num is greater than
> else
> echo $num is less than
> fi
>
> But I need to know if $num and $another_num are equal too.
>
> Can I just have the function return a value (other than the status)
> that could be tested?
> How could I do that? Thx for your .
>


I don't know if this would work, but I'd type it in and test it.

if ( a < b ) ? -1 : ( a == b ) ? 0 : 1 ;

Or is this one of those thing that is NOT portable anywhere except
inside my head?

Dave
  Réponse avec citation
Vieux 28/10/2006, 13h00   #5
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: return values from a function call

Dave Kelly wrote:
> paintedjazz@gmail.com wrote:
>
>> If I use the result of a function call in a condtional statement,
>> is it possible to return one of three possible values or is it just
>> true or false or a 0 or 1 status?
>>
>> I want to test if a number is greater than, equal to or less than
>> another number.
>> Thus, the function should be something like:
>>
>> if myfunction $num $another_num # return value is probably only 0 or
>> 1
>> then
>> echo $num is greater than
>> else
>> echo $num is less than
>> fi
>>
>> But I need to know if $num and $another_num are equal too.
>>
>> Can I just have the function return a value (other than the status)
>> that could be tested?
>> How could I do that? Thx for your .
>>

>
> I don't know if this would work, but I'd type it in and test it.
>
> if ( a < b ) ? -1 : ( a == b ) ? 0 : 1 ;
>
> Or is this one of those thing that is NOT portable anywhere except
> inside my head?


Don't know which shell supports your syntax. In ksh93 you can write...

x=$(( (a < b)? -1 : (a == b)? 0 : 1 ))


Janis

>
> Dave

  Réponse avec citation
Vieux 29/10/2006, 01h56   #6
Dave Kelly
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: return values from a function call

Janis Papanagnou wrote:
> Dave Kelly wrote:
>> paintedjazz@gmail.com wrote:
>>
>>> If I use the result of a function call in a condtional statement,
>>> is it possible to return one of three possible values or is it just
>>> true or false or a 0 or 1 status?
>>>
>>> I want to test if a number is greater than, equal to or less than
>>> another number.
>>> Thus, the function should be something like:
>>>
>>> if myfunction $num $another_num # return value is probably only 0 or
>>> 1
>>> then
>>> echo $num is greater than
>>> else
>>> echo $num is less than
>>> fi
>>>
>>> But I need to know if $num and $another_num are equal too.
>>>
>>> Can I just have the function return a value (other than the status)
>>> that could be tested?
>>> How could I do that? Thx for your .
>>>

>>
>> I don't know if this would work, but I'd type it in and test it.
>>
>> if ( a < b ) ? -1 : ( a == b ) ? 0 : 1 ;
>>
>> Or is this one of those thing that is NOT portable anywhere except
>> inside my head?

>
> Don't know which shell supports your syntax. In ksh93 you can write...
>
> x=$(( (a < b)? -1 : (a == b)? 0 : 1 ))


I don't know which shell supports it either. I wrote it using an example
from my K&R C language book.

The OP does not have to use, minus one, zero, one. He can use any value
that will fit in 'a' or 'b'. Does this take on the same type as in C code?

And does this fill the need of the OP?
  Réponse avec citation
Vieux 29/10/2006, 13h07   #7
paintedjazz@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: return values from a function call


Janis Papanagnou wrote:
> paintedjazz@gmail.com wrote:
> > If I use the result of a function call in a condtional statement,
> > is it possible to return one of three possible values or is it just
> > true or false or a 0 or 1 status?
> >
> > I want to test if a number is greater than, equal to or less than
> > another number.
> > Thus, the function should be something like:
> >
> > if myfunction $num $another_num # return value is probably only 0 or
> > 1
> > then
> > echo $num is greater than
> > else
> > echo $num is less than
> > fi
> >
> > But I need to know if $num and $another_num are equal too.
> >
> > Can I just have the function return a value (other than the status)
> > that could be tested?
> > How could I do that? Thx for your .
> >

>
> You could of course return values of 0, 1, and 2 from the function,
> then test the return code through variable $? in a case statement.
>
> fn() { ...
> return $anyresult
> }
>
> case $? in
> ...
> esac
>
> But the intention of the exit code is rather to indicate success
> (code 0) or failure (code >0), and conceptually the shell functions
> are more like commands than like mathematical functions. Values from
> functions are usually returned through the "print interface"...
>
> fn() { ...
> print $anyresult
> }
>
> result=$( fn arg1 arg2 )
> if [ $result -eq ... ]
>
> Or use a case statement...
>
> case $( fn arg1 arg2 ) in
> ...
> esac
>
>
> Janis


I'm not sure what the"print interface" is. Do you mean you just echo
or printf a string to stdout and that will end up as the result of the
function? I do like this way of doing it though.

  Réponse avec citation
Vieux 29/10/2006, 13h13   #8
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: return values from a function call

paintedjazz@gmail.com wrote:
> Janis Papanagnou wrote:
>
>>paintedjazz@gmail.com wrote:
>>
>>>If I use the result of a function call in a condtional statement,
>>>is it possible to return one of three possible values or is it just
>>>true or false or a 0 or 1 status?
>>>
>>>I want to test if a number is greater than, equal to or less than
>>>another number.
>>>Thus, the function should be something like:
>>>
>>>if myfunction $num $another_num # return value is probably only 0 or
>>>1
>>>then
>>> echo $num is greater than
>>>else
>>> echo $num is less than
>>>fi
>>>
>>>But I need to know if $num and $another_num are equal too.
>>>
>>>Can I just have the function return a value (other than the status)
>>>that could be tested?
>>>How could I do that? Thx for your .
>>>

>>
>>You could of course return values of 0, 1, and 2 from the function,
>>then test the return code through variable $? in a case statement.
>>
>> fn() { ...
>> return $anyresult
>> }
>>
>> case $? in
>> ...
>> esac
>>
>>But the intention of the exit code is rather to indicate success
>>(code 0) or failure (code >0), and conceptually the shell functions
>>are more like commands than like mathematical functions. Values from
>>functions are usually returned through the "print interface"...
>>
>> fn() { ...
>> print $anyresult
>> }
>>
>> result=$( fn arg1 arg2 )
>> if [ $result -eq ... ]
>>
>>Or use a case statement...
>>
>> case $( fn arg1 arg2 ) in
>> ...
>> esac
>>
>>
>>Janis

>
>
> I'm not sure what the"print interface" is. Do you mean you just echo
> or printf a string to stdout and that will end up as the result of the
> function? I do like this way of doing it though.
>


Yes, exactly. Sorry for having been unclear. And I shouldn't have used
the ksh specific print; you should indeed use printf (not echo).

Janis
  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 13h34.


É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,18462 seconds with 16 queries