swapping stdout & stderr, OK?

swapping stdout & stderr, OK?

Post by Dave Plon » Mon, 08 Jun 1992 03:30:48



In a particular process which forks 1 child, I would like to have the
parent log to stdout and the child to stderr both using functions which
typically write to stdout (like puts(), and printf()).
Then when I launch the program, I can use redirection to specify the files
to which I log, rather than adding parameters to the programs. Also, then,
if I run it interactively, both processes' output goes to my terminal.

Here's what I've done:

#include <my_environ.h>

#define STDOUT 1
#define STDERR 2

   int saved_fp;
   if ((saved_fp = dup(STDERR)) == -1) exit(1);
   close(STDERR);
   if (dup(STDOUT) != STDERR) exit(1);
   close(STDOUT);
   if (dup(saved) != STDOUT) exit(1);
   close(saved_fp);

and... it works. Afterward, puts() goes to stderr, and fprintf(stderr,) goes
to stdout (from the shell's perspective anyway).

However, I'm wondering if it is legal to do this, ie. rely
on dup() to return the lowest fd after closing stdout, for instance. (I assumed
stdin fd was unavailable.) Is it OK to close stdout and stderr in this way?

Please email. Thanks,
Dave

 
 
 

1. stderr & stdout to file and stderr to screen

Within a (bash) script I want to log standard output and standard error
to a log file. Ok, I used something like

  prog >> logfile 2>&1

But now I want to see the error messages (and ONLY the error messages)
also on my screen. I think "tee" and the use of further filedescriptors
are proper tools but how it works really ?

Thanks

Jens

--
Dr. Jens Schleusener            debis Systemhaus
phone: +49 (551) 709-2493       Solutions for Research
fax:   +49 (551) 709-2169       Bunsenstr.10

2. Selling: DS10 Unix Unlimited

3. ksh redirect stdout & stderr > file1, stderr > file2

4. ip-up scripts waiting to finish sendmail

5. Please clarify the use of stdout && stderr in this example

6. possible absolute pathname bug in ld.so-1.4

7. swap stderr and stdout

8. lpd/lpr printing

9. Redirecting stdout and stderr, and stderr

10. file descriptor Q, redirecting stderr and stdout to stdout and file.

11. Joining stdout & stderr of subprocess ?

12. stdout & stderr direction in "ksh"

13. Logging stdout & stderr