On Mon, Jul 28, 2003 at 07:15:07PM +1000, Andy Eager wrote:
> Hi all,
> 
> Got a bit of a C code using execle() to call a shell script with a 
> specific environment.  I've got no trouble seeing the environment vars 
> in bash.
> 
> Is there any way of setting an environment variable in the shell script 
> so that it modifies the environment of the *parent* process?
> I fear not - but is there any way of returning something from a shell 
> other than through an exit status?

My C is a bit rusty, so someone please correct me if I'm wrong, but if I
recall correctly...

You fear correctly -- a child can't modify its parent's environment.

There are other ways to communicate between processes, though.  One option
that springs to mind is to open a pipe(2) in the parent before you fork and
exec, then the child could write to the pipe, and the parent could read from
it.  If you create a pair of pipes, and in the child (before forking) use
e.g. dup2(2) to copy their file descriptors to 0 and 1, then you've just
created a stdin and a stdout for the child to use, then you should just be
able to use 'echo' and so on in your shell script without any problems. :)

You probably should create a third pipe for stderr, too -- although you
could probably get away with re-using the stdout pipe.

-Andrew.

-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug

Reply via email to