|
|>>I can't seem to make a bash shell function work as I expect (darn
|>>expectations anyway...) which is to return a value where the function is
|>>called:
|> ..
|>># Gotta do this?
|>>bar
|>>declare -i foo=$?
|>
|> Generally it isn't necessary to use "declare -i".
|
| According to OReilly book, this creates the variable as type integer.
| Probably not necessary, but a hint to readers of what to expect.
|
|
|> bar
|> foo=$?
|>
|> The above saves the return status for use later, and for immediate use,
|> one example is
|
|
|
| Let's see if I've got this right : BASH "functions" are not callable using
| the same paradigm as other scripting or programming languages ( except for
| assembly). In this respect they are really "subroutines". They *do*
| store a numeric return value in a system variable which the caller then
| accesses, but calling a BASH function and picking up the returned value
| must be a 2-step process:
|
| 1. Call the function
| 2. Retrieve or assign the returned result.
|
| In most other languages, the act of retrieving or assigning a function
| result is performed by the same step that calls the function. Not so in
| BASH. This behaviour can confuse the BASH novice, who might hear the word
| "function", and use their experience to provide an operational context of
| what a function is, and how it is used. To make life interesting, this
| dichotomy is ignored by all documentation.
It depends on the use to which you will be putting the function.
If you are goung to be using it to return a boolean type yes/no response
to determine program flow, then user the "return" command at the end to
set the appropriate status value for the function.
If you are using it to compute a numeric or string value from inputs for
use in further computations, the use "echo" to set the output value.
(You should still set the "return" to a non-zero value in case of an error
condition, which you can check for as well as using the real answer.)
For example:
$ function bar
{
echo 123
Quote:}
$ declare -i foo=$(bar)
$ echo $foo
123
--
Reverend Paul Colquhoun, ULC. http://andor.dropbear.id.au/~paulcol
Asking for technical help in newsgroups? Read this first:
http://www.tuxedo.org/~esr/faqs/smart-questions.html