Find the answer to your Linux question:
Results 1 to 7 of 7
Any way to redirect error output to two separate files....
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Posts
    15

    Question Redirecting error output


    Any way to redirect error output to two separate files.

  2. #2
    Just Joined!
    Join Date
    Mar 2007
    Posts
    16
    Hi psandeepnair1985,

    I do not believe there is a way to do this without running the same command twice or making a copy of the error file if it exists. Once standard error is redirected, it is no longer available for further processing. This is unlike standard output, where you can use the tee command. In a script file, you could check to see if an error file is generated and then copy it to another name to get another copy. If you can provide more detail on why you need to do this, maybe I can be more helpful.

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,283
    Hi.

    Redirecting STDERR to STDOUT and discarding STDOUT can allow this:
    Code:
    #!/bin/sh
    
    # @(#) s1       Demonstrate redirection of STDERR.
    
    # Remove files.
    
    rm -f file1 file2
    
    echo
    echo " Helper script writes text to STDOUT and STDERR:"
    ./h1
    
    echo
    echo " STDERR directed to STDOUT, and STDOUT discarded:"
    ./h1 2>&1 1>/dev/null
    
    echo
    echo " STDERR redirected to STDOUT, replicated to 2 files and"
    echo " to STDOUT with tee:"
    echo " (order of redirection is important here.)"
    ./h1 2>&1 1>/dev/null |
    tee file1 file2
    
    echo
    echo " Contents of file1 and file2:"
    cat file1 file2
    
    echo
    echo " Counts of content:"
    wc file1 file2
    
    exit 0
    Produces:
    Code:
    % ./s1
    
     Helper script writes text to STDOUT and STDERR:
    This message is written on STDOUT
    This message is written on STDERR
    
     STDERR directed to STDOUT, and STDOUT discarded:
    This message is written on STDERR
    
     STDERR redirected to STDOUT, replicated to 2 files and
     to STDOUT with tee:
     (order of redirection is important here.)
    This message is written on STDERR
    
     Contents of file1 and file2:
    This message is written on STDERR
    This message is written on STDERR
    
     Counts of content:
     1  6 34 file1
     1  6 34 file2
     2 12 68 total
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Just Joined!
    Join Date
    Mar 2007
    Posts
    16
    Hi drl,

    What I love about Unix is that you learn something new everyday and that where there is a will, there is a way. I successfully tested your code with the following, where bunk is a non-existent file:
    cat bunk 2>&1 >/dev/null | tee file 1 file2
    The error message appears on STDOUT and in file1 and file2. Two questions for you:
    1) What if you do not want the error message to appear in STDOUT, but want it to go to two separate files?
    2) The solution works, but does not make sense. 2>&1 says to send STDERR the same place as STDOUT. If you are sending STDOUT to the Unix garbarge can and STDERR goes to the same place, how is the error message getting to the tee command?

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,283
    Hi, dkloes.
    Quote Originally Posted by dkloes
    ... 1) What if you do not want the error message to appear in STDOUT, but want it to go to two separate files?
    How would you usually throw away STDOUT?

    Quote Originally Posted by dkloes
    2) The solution works, but does not make sense. 2>&1 says to send STDERR the same place as STDOUT. If you are sending STDOUT to the Unix garbarge can and STDERR goes to the same place, how is the error message getting to the tee command?
    Is a/b always the same as b/a?

    In O'Reilly Learning the bash Shell, Chapter 7, I/O Redirectors:
    ... for the whole story, look at the entries for read(), write(), fcntl() and others in Section 2 of the UNIX manual.

    ... 1> is the same as >, and 0< is the same as <. If you understand this then you probably know all you need to know about file descriptors.
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    Just Joined!
    Join Date
    Mar 2007
    Posts
    16
    Hi drl,

    Sorry, it appears that these were inappropriate follow-up questions in this forum. If so, I apologize. The reason that I asked these is that I have been working with Unix since 1980 and even wrote a book on shell programming, but you threw some new twists here that are interesting and change some of my understanding of redirection. It would have been helpful if you could have answered the two questions directly.

  7. #7
    Just Joined!
    Join Date
    Mar 2007
    Posts
    16
    Hi psandeepnair1985,

    Here is a further explanation based on drl's post with a bit more explanation. This will also help to clarify my understanding of how redirection works. drl can correct if any of this is not stated properly:

    echo " STDERR directed to STDOUT, and STDOUT discarded:"
    ./h1 2>&1 1>/dev/null


    STDOUT (represented by file descriptor 1) is what normally comes to the screen (not including an error message)
    Anything redirected to /dev/null is discarded
    STDERR (represented by file descriptor 2) would be the error message
    2>&1 says to direct STDERR to STDOUT, so STDERR now becomes STDOUT
    STDOUT is discarded with 1>/dev/null or >/dev/null (the 1 can be omitted because it is assumed).
    Since STDERR is now STDOUT, the error message comes to the screen
    The order is important. If the arguments are reversed, STDOUT is discarded and so is the error message (./h1 1>/dev/null 2>&1). In this case, there would be no output to the screen.

    echo " STDERR redirected to STDOUT, replicated to 2 files and"
    echo " to STDOUT with tee:"
    echo " (order of redirection is important here.)"
    ./h1 2>&1 1>/dev/null |
    tee file1 file2


    Since STDERR replaces STDOUT as explained earlier, it can be piped to the tee command for further processing. tee is used whenever we want output to go to more than one place. In this case, the error message is sent to the screen and to file1 and file2.
    If you want the error message to go to both files, but not to the screen, you would discard it at the end:
    ./h1 2>&1 1>/dev/null | tee file1 file2 > /dev/null

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •