Re: [SLUG] C - exec fn call env vars

2003-07-29 Thread mlh
The unix programming faq discusses this.
It's not really a C question.
-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug


[SLUG] C - exec fn call env vars

2003-07-28 Thread Andy Eager
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?

Thanks,

Andy

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


Re: [SLUG] C - exec fn call env vars

2003-07-28 Thread Stuart Cooper
 
 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?

no there isn't. in a shell script if you want to change the enviornment
you need to do
. script
which actually redirects standard input of the shell to come from script
and reads it in line at a time. A lot of shell commands like cd are
called builtins because they operate in the current process and don't
form subprocesses... because they affect the parent shell process.

The classic Kernighan  Pike book The Unix Programming Environment explains
this at length; there are of course good free references.

 I fear not - but is there any way of returning something from a shell 
 other than through an exit status?

Parent and child can co-ordinate in various ways... the obviously easy one
is for the child to write to a temp file and the parent to read the
info from that file and then delete the file; but there are other ways
to do it; this falls under the topic of Interprocess Communication. You 
can install signal handlers in the parent such that when a child terminates
the parent goes and checks interesting things; you also want to look
at the wait() system call.

Hope this helps,
Stuart.
-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug


Re: [SLUG] C - exec fn call env vars

2003-07-28 Thread Angus Lees
At Mon, 28 Jul 2003 19:15:07 +1000, Andy Eager wrote:
 Is there any way of setting an environment variable in the shell script 
 so that it modifies the environment of the *parent* process?

nope.

 I fear not - but is there any way of returning something from a shell 
 other than through an exit status?

stdout (or some other file descriptor), some file somewhere.  all the
other types of IPC too, but they're a bit hard from a shell script
(without a helper (non-shell) command).

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


Re: [SLUG] C - exec fn call env vars

2003-07-28 Thread Andrew Bennetts
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