Re: return values from a function call
- From: paintedjazz@xxxxxxxxx
- Date: 29 Oct 2006 05:07:23 -0800
Janis Papanagnou wrote:
paintedjazz@xxxxxxxxx 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
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.
.
- Follow-Ups:
- Re: return values from a function call
- From: Janis Papanagnou
- Re: return values from a function call
- References:
- return values from a function call
- From: paintedjazz
- Re: return values from a function call
- From: Janis Papanagnou
- return values from a function call
- Prev by Date: Re: Using the grep command to filter
- Next by Date: Re: return values from a function call
- Previous by thread: Re: return values from a function call
- Next by thread: Re: return values from a function call
- Index(es):
Relevant Pages
|