bash redirection question

Bob Bell bbell at hp.com
Fri Nov 7 22:26:43 EST 2003


On Fri, Nov 07, 2003 at 05:15:30PM -0500, Kevin D. Clark wrote:
> Steven W. Orr writes:
> > Another way to go is for your C program to fork and exec a child process 
> > to be the tee. Then the shell script would also inherit the stdout/stderr 
> > descriptors which are also shared (inherited) by the tee process.
> 
> You lost me here.  I'm pretty sure that anything down this particular
> path won't work.

I think Steven's mentioning a method that is similar to what I wrote
(http://mail.gnhlug.org/pipermail/gnhlug-discuss/2003-November/005664.html).
However, with Steven's method, the invoking C program wouldn't have to
manage the output from the shell script.  Instead, early on the
C program would create a pipe(), fork() a new process, dup2() the read
side of the pipe to the child process's stdin (fd 0), and then exec()
tee with the log file as a parameter.  The parent process would then
dup2() the write side of the pipe to stdout (fd 1) and stderr (fd 2)
(unless the program really needed to keep around the "real" stdout and
stderr).  Now, whenever the C program prints to its new "stdout", the
text gets sent to the "stdin" of the child process running tee, which
both prints to screen and to the logfile.  The bash shell script,
invoked by fork()+exec() or system(), inherits the remapped "stdout" and
"stderr" of the C program, and will do the same.

-- 
Bob Bell



More information about the gnhlug-discuss mailing list