tobold.org

correct • elegant • free

△ comp.unix.shell △

◅ Symbol Table

RSHing a daemon ▻

Calling functions and passing variables?

In article <7s5ca9$e69$1@nnrp1.deja.com>,  <srhadden@my-deja.com> wrote:
>Ok, but when you say that the changes after the fork are not seen by the
>other, you mean the other function, func_2, BUT the changes ARE
>reflected inside the "MAIN" area.  Why is this, if a fork causes a COPY
>of the memory, then why are the changes to variables inside the forked
>function reflected in the main (entry-point) area?

They're not.

In your example script, ksh forks to evaluate myfunc1 in the background.
In this fork, no changes are made to TESTVAR, and it eventually prints
`OLD'.

Then, ksh evaluates myfunc2 without forking, and both the function and
the "top level" report the new value of TESTVAR.

So when I run your script, I see this.  Note that the top level ksh
has finished, and control returned to my login shell before myfunc1
completes.

    $ ksh script
    f2: myfunc-2
    myfunc-2
    $ f1: OLD

Shell functions are normally evaluated in the current shell; it is
only the `&' that caused myfunc1 to be evaluated in a different shell.
(Functions in other shell constructs such as pipelines will also be
evaluated in a child shell.)

Tim.
--
Tim Goodwin   | "[ `static' ] is so overloaded in the C/C++ world that
Leicester, UK | I try to avoid it whenever I can't." -- Larry Wall

Original headers:

From: tjg@star.le.ac.uk (Tim Goodwin)
Newsgroups: comp.unix.shell
Subject: Re: Calling functions and passing variables?
Date: 21 Sep 1999 11:49:16 +0100
Organization: University of Leicester, UK
Message-ID: <7s7ns2$2fj$1@ltpcg.star.le.ac.uk>
References: <7rtp05$eke$1@nnrp1.deja.com> <7rudad$u6h$1@nnrp1.deja.com>
  <1%yE3.138$zE6.6725@burlma1-snr2> <7s5ca9$e69$1@nnrp1.deja.com>

△ comp.unix.shell △

◅ Symbol Table

RSHing a daemon ▻