It is currently Tue, 30 Nov 2021 16:56:54 GMT



 
Author Message
 functions in Bourne shell

Hi,

Could somebody pl. tell me how could I get the return value
of a function.

Eg. say i have a function like

foo ()
{
        return $value

in the main script I want to get the value. What could be the
correct syntax for that. Is it something like this.

return_value=`foo`

Thanks,

..Senthil

"It's not hard to meet expenses, they're everywhere.."



 Tue, 30 Mar 1999 03:00:00 GMT   
 functions in Bourne shell

%% Senthilvel Rangaswamy <sent...@ece.vill.edu> writes:

  sr> Could somebody pl. tell me how could I get the return value
  sr> of a function.

Treat functions just like "normal" executables you would run.  How would
you get the "return" value of an executable?

The return value is the exit code (note return values can only be
numeric!), so use $? to find it.

  sr> foo ()
  sr> {
  sr>        return $value
  sr> }

  return_value=$?

  sr> in the main script I want to get the value. What could be the
  sr> correct syntax for that. Is it something like this.

  sr> return_value=`foo`

No.  Just like with normal programs, backticks are replaced with the
_output_ of the function; that is, whatever the function writes to
stdout (in this case, nothing).

--
-------------------------------------------------------------------------------
 Paul D. Smith <psm...@baynetworks.com>         Network Management Development
 Senior Software Engineer                                   Bay Networks, Inc.
-----------------------------------------------==<http://www.baynetworks.com/>-
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
     These are my opinions--Bay Networks takes no responsibility for them.



 Tue, 30 Mar 1999 03:00:00 GMT   
 functions in Bourne shell

: Hi,

: Could somebody pl. tell me how could I get the return value
: of a function.

: Eg. say i have a function like

: foo ()
: {
:       return $value
: }

: in the main script I want to get the value. What could be the
: correct syntax for that. Is it something like this.

: return_value=`foo`

Close.  In the main script call foo and then reference $value
directly.

foo

[ foo -eq .. ] && .....

For examples of how SCO uses the getyn function (answer y or n),
grep /etc/*
It is used in a number of different shell scripts.

: Thanks,

: ..Senthil

: "It's not hard to meet expenses, they're everywhere.."

--
Harold Knopke                                 h...@freenet.edmonton.ab.ca
Aspen Computins Solutions                     h...@aspen.ab.ca



 Tue, 30 Mar 1999 03:00:00 GMT   
 functions in Bourne shell

: Senthilvel Rangaswamy (sent...@ece.vill.edu) wrote:

: : Hi,

: : Could somebody pl. tell me how could I get the return value
: : of a function.

: : Eg. say i have a function like

: : foo ()
: : {
: :     return $value
: : }

: : in the main script I want to get the value. What could be the
: : correct syntax for that. Is it something like this.

: : return_value=`foo`

: Close.  In the main script call foo and then reference $value
: directly.

: foo

: [ foo -eq .. ] && .....
    ^^^ - whoops, that should have read $value. Sorry.

: For examples of how SCO uses the getyn function (answer y or n),
: grep /etc/*
: It is used in a number of different shell scripts.

: : Thanks,

: : ..Senthil

: : "It's not hard to meet expenses, they're everywhere.."

: --
: Harold Knopke                                 h...@freenet.edmonton.ab.ca
: Aspen Computins Solutions                     h...@aspen.ab.ca

--
Harold Knopke                                 h...@freenet.edmonton.ab.ca
Aspen Computins Solutions                     h...@aspen.ab.ca



 Tue, 30 Mar 1999 03:00:00 GMT   
 functions in Bourne shell

This will work* as long as your functions are returning numeric values. If
you want to return a string, do this.

Sometimes, I want to return a string. In this case, you should do this:

    foo ()
    {
        #whatever
        echo "$value"
    }

In the main program, you can then do this:

    value="`foo`"

Then you can test the string $value. Of course, you could do this too:

    if [ "`foo`" = "mystring ]
    then
        command
    fi

or

    case "`foo`" in

        string1)
            ;;
        string2)
            ;;
        *)
    esac

--
*There is a problem with the original solution. "$value" is undefined when
you return from the function in most shells. You need to use $? which
gives you the exit value of the last function or command executed:

    [ $? -eq .. ] && <command>

or even better

    foo && <command>       #If $? = 0, <command> will execute

or
    if foo
    then
        <command>
    fi

--
David Weintraub                    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Deutsche Morgen Grenfell          _/                                      _/
dw...@dbna.com                   _/    I AM THE GREAT AND POWERFUL OZ*   _/
dav...@cnj.digex.net            _/                                      _/
                               _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
                          *(Pay no attention to the man behind the curtains)



 Wed, 31 Mar 1999 03:00:00 GMT   
 
   [ 5 post ] 

Similar Threads

1. aritmetic functions in Bourne shell

2. Challange: increment function in Bourne Shell

3. Functions in bourne shell: HP and the rest of the world

4. Functions in Bourne Shell

5. Looking for "DOSKEY" function under Bourne shell

6. code for "read" function of bourne shell

7. question on 'read' function with Bourne Shell

8. sh: how to write recursive functions in Bourne shell?

9. Light Speed Bourne Shell! (was: Bourne shell tricks)


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software