It is currently Tue, 07 Dec 2021 03:03:35 GMT

Author Message
 Redirect stdout * stderr to one file in bourne sh using exec
I've been trying to figure out how to redirect all output (stdout & stderr)
to one file from within a bourne shell script.  I want to accomplish this
without having to redirect the entire scripts output on the command line and
also without appending a redirect to each line of the script that generates

I've been able to get the stdout redirected with the following snippet that
I stripped down from an example using tee (I only want output to go to the
file no terminal output at all).

exec 3>&1 > logfile
echo test

All stdout now goes to logfile.  (Although I admit to not fully understand
the redirect as it seems backwards redirecting descriptor 3 to stdout and
then stdout to logfile).  I am able to get close to what I want with the
following (which seems even more backwards):

exec 3>&1 > stderr
exec 3>&2 > stdout
echo test

Now stdout contains "test" and stderr contains "asdasd: not found" but the
redirections seem to be opposite of what they should be (1 - stdout, 2 -
stderr).  Additionally if I use one filename for bot redirections all the
file ever contains is stderr, never stdout.

Is this possible?

This is using Bourne shell on Solaris 8.  Redirecting the entire scripts
contents from the command line invocation does not meet our requirements.
Any help or pointers to a good reference would be appreciated.

 Tue, 09 Mar 2004 02:57:16 GMT   
 Redirect stdout * stderr to one file in bourne sh using exec

Better use:

exec 3> stderr 2>&3
exec 3> stdout 1>&3


exec 3> stderr 1>&3 2>&3


Cheers, jsaul

 Tue, 09 Mar 2004 04:08:26 GMT   
 Redirect stdout * stderr to one file in bourne sh using exec

I may misunderstand what you want, but from that description all
you need is this:

exec >logfile 2>&1

First >logfile directs stdout to file, then 2>&1 redirects stderr
to wherever stdout is going at that point. What more do you need?

The only effect of "3>&1" there is saving old stdout, i.e., if you want
to output something to original stdout later you can do it with >&3.
If you don't need that, "exec >logfile" is enough.

In general, "n>&m" means copying current descriptor m to n;
subsequent redirection of m will not change n.

Thus, if you want to do the redirection for a portion of the script only
and then restore both stdout and stderr as they were, it can be done
like this:

exec 3>&1 4>&2 >logfile 2>&1
# original stdout saved in &3 and stderr in &4
# commands with stdout and stderr going to logfile
exec 1>&3 2>&4

Really? That shouldn't happen (and didn't happen when I tried it).
I get "test" in stdout as expected but the error message is not

Are you sure the spaces are exactly as shown above?
Or is something else missing (like, should the second
redirection be "3&>2 2> stdout" or something)?
If not, your sh is broken.

Whenever you do n>filename the file is trashed (overwritten).
If you want to redirect a filehandle to an already open file,
use the n>&m syntax.

Tapani Tarvainen

 Tue, 09 Mar 2004 13:40:38 GMT   
 Redirect stdout * stderr to one file in bourne sh using exec

Here's a cut & paste of my script and the output

rm -f stdout stderr
exec 3>&1 > stderr
exec 2>&1 > stdout

echo test

./ asdasd: not found


But the "exec > logfile 2>&1" works as exected.

Thanks to all

 Tue, 09 Mar 2004 20:51:15 GMT   
   [ 4 post ] 

Similar Threads

1. How to redirect stderr (not stdout) coming from tar in csh/sh

2. Redirecting output of one command to both stdout and stderr

3. Bourne Shell: stdout and stderr to same file ?

4. Redirecting stdout and stderr, and stderr

5. redirection: stdout to one file, stderr to other file, and both to screen

6. bash : how to redirect both stdout and stderr to append a file

7. Redirect stderr and stdout in two separate files?

8. Redirecting stdout and stderr to file as well as screen for Korn shell

9. Redirecting stderr & stdout to file and screen

10. Problem with redirecting stdout and stderr to log file

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software