bash redirection question

Kevin D. Clark kclark at CetaceanNetworks.com
Fri Nov 7 17:15:30 EST 2003


Steven W. Orr writes:

> If you use the
> above proposed call to system then only the shell script would be logging
> to the logfile.

Oh, now that I re-read Ken's email, I guess that's what he wants.  I
stand corrected.

[snip]
> Another trick to use is to script it. See the script(1) command. It will 
> capture *everything* in a file.

I'd probably use script here too.

> First you check an environment variable when you start your C 
> program. If it's not set then set it and re-exec yourself but this time 
> using the pipe to tee trick. (You could also use a program option like -s 
> to get the same message across.
> 
> if ( ! getenv ( "HaveIBeenHereBefore" ) )
> {
>     setenv ( "HaveIBeenHereBefore", "1", 1 );
>     /* This assumes that you are called with one arg. */
>     sprintf ( cmd, "%s %s 2>&1 | tee %s", argv[0], argv[1], logfile );
>     system ( cmd );
> }
> /* Keep on going. */
> blah blah blah;

Since we're being pedantic today, I will point out that there's no
guarantee that argv[0] contains anything useful.  Practically
speaking, this probably isn't something to worry too much about.

> 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.
> 
> Make sense?

You lost me here.  I'm pretty sure that anything down this particular
path won't work.

Regards,

--kevin
-- 
"More than any other time in history, mankind faces a crossroads. One
path leads to despair and utter hopelessness. The other, to total
extinction. Let us pray we have the wisdom to choose correctly."
--Woody Allen




More information about the gnhlug-discuss mailing list