Unix Shell - return values from a function call

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > return values from a function call





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author return values from a function call
paintedjazz@gmail.com

2006-10-28, 1:30 am

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

Chris F.A. Johnson

2006-10-28, 1:30 am

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


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
Janis Papanagnou

2006-10-28, 1:30 am

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


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
Dave Kelly

2006-10-28, 1:30 am

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


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
Janis Papanagnou

2006-10-28, 7:24 am

Dave Kelly wrote:
> paintedjazz@gmail.com wrote:
>
>
> 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

Dave Kelly

2006-10-29, 1:36 am

Janis Papanagnou wrote:
> Dave Kelly wrote:
>
> 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?
paintedjazz@gmail.com

2006-10-29, 7:17 am


Janis Papanagnou wrote:
> paintedjazz@gmail.com wrote:
>
> 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.

Janis Papanagnou

2006-10-29, 7:17 am

paintedjazz@gmail.com wrote:
> Janis Papanagnou wrote:
>
>
>
> 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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com