redirect stderr to two different files - Unix

This is a discussion on redirect stderr to two different files - Unix ; Hi all, I wish to redirect stdout and stderr to the same file and also redirect stderr to a 2nd file. E.g. someprocess >outfile 2>&1 gets me both stdout and stderr into outfile but how do I get stderr into ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: redirect stderr to two different files

  1. redirect stderr to two different files

    Hi all,

    I wish to redirect stdout and stderr to the same file and also
    redirect stderr to a 2nd file. E.g.

    someprocess >outfile 2>&1

    gets me both stdout and stderr into outfile but how do I get stderr
    into a second file, errfile as well as outfile?

    Test program;

    #include

    int main()
    {
    fprintf(stdout, "standard output (1)\n");
    fprintf(stderr, "standard error (2)\n");
    return 0;
    }

  2. Re: redirect stderr to two different files

    On Oct 16, 9:54*am, johnmmcparland
    wrote:
    > Hi all,
    >
    > I wish to redirect stdout and stderr to the same file and also
    > redirect stderr to a 2nd file. *E.g.
    >
    > someprocess >outfile 2>&1
    >
    > gets me both stdout and stderr into outfile but how do I get stderr
    > into a second file, errfile as well as outfile?
    >
    > Test program;
    >
    > #include
    >
    > int main()
    > {
    > * * fprintf(stdout, "standard output (1)\n");
    > * * fprintf(stderr, "standard error (2)\n");
    > * * return 0;
    >
    > }


    I should add I'm using korn shell.

  3. Re: redirect stderr to two different files

    johnmmcparland writes:
    >> I wish to redirect stdout and stderr to the same file and also
    >> redirect stderr to a 2nd file. *E.g.
    >>
    >> someprocess >outfile 2>&1
    >>
    >> gets me both stdout and stderr into outfile but how do I get stderr
    >> into a second file, errfile as well as outfile?
    >>
    >> Test program;
    >>
    >> #include
    >>
    >> int main()
    >> {
    >> * * fprintf(stdout, "standard output (1)\n");
    >> * * fprintf(stderr, "standard error (2)\n");
    >> * * return 0;
    >>
    >> }

    >
    > I should add I'm using korn shell.


    I have no ksh here, but the process substitution feature of bash as
    basically been stolen from it, so the following might/ should work
    with ksh, too:

    [rw@fever]/tmp/tt $cat b.c
    #include

    int main(void)
    {
    puts("stdout");
    fputs("stderr\n", stderr);
    return 0;
    }
    [rw@fever]/tmp/tt $cc b.c
    [rw@fever]/tmp/tt $rm e o
    rm: cannot remove `e': No such file or directory
    rm: cannot remove `o': No such file or directory
    [rw@fever]/tmp/tt $./a.out >o 2> >(tee e)
    [rw@fever]/tmp/tt $cat o
    stdout
    stderr
    [rw@fever]/tmp/tt $cat e
    stderr

  4. Re: redirect stderr to two different files

    On Thursday 16 October 2008 10:54, johnmmcparland wrote:

    > Hi all,
    >
    > I wish to redirect stdout and stderr to the same file and also
    > redirect stderr to a 2nd file. E.g.
    >
    > someprocess >outfile 2>&1
    >
    > gets me both stdout and stderr into outfile but how do I get stderr
    > into a second file, errfile as well as outfile?
    >
    > Test program;
    >
    > #include
    >
    > int main()
    > {
    > fprintf(stdout, "standard output (1)\n");
    > fprintf(stderr, "standard error (2)\n");
    > return 0;
    > }


    someprocess 2>&1 > outfile | tee -a outfile > errfile

    Although I'm not sure whether the order in which the above will print lines
    inside outfile is deterministic.


  5. Re: redirect stderr to two different files

    On Oct 16, 10:37*am, Rainer Weikusat wrote:
    > johnmmcparland writes:
    > >> I wish to redirect stdout and stderr to the same file and also
    > >> redirect stderr to a 2nd file. *E.g.

    >
    > >> someprocess >outfile 2>&1

    >
    > >> gets me both stdout and stderr into outfile but how do I get stderr
    > >> into a second file, errfile as well as outfile?

    >
    > >> Test program;

    >
    > >> #include

    >
    > >> int main()
    > >> {
    > >> * * fprintf(stdout, "standard output (1)\n");
    > >> * * fprintf(stderr, "standard error (2)\n");
    > >> * * return 0;

    >
    > >> }

    >
    > > I should add I'm using korn shell.

    >
    > I have no ksh here, but the process substitution feature of bash as
    > basically been stolen from it, so the following might/ should work
    > with ksh, too:
    >
    > [rw@fever]/tmp/tt $cat b.c
    > #include
    >
    > int main(void)
    > {
    > * * * * puts("stdout");
    > * * * * fputs("stderr\n", stderr);
    > * * * * return 0;}
    >
    > [rw@fever]/tmp/tt $cc b.c
    > [rw@fever]/tmp/tt $rm e o
    > rm: cannot remove `e': No such file or directory
    > rm: cannot remove `o': No such file or directory
    > [rw@fever]/tmp/tt $./a.out >o 2> >(tee e)
    > [rw@fever]/tmp/tt $cat o
    > stdout
    > stderr
    > [rw@fever]/tmp/tt $cat e
    > stderr


    I'm afraid that doesn't seem to work in ksh

    ../output >o 2> >(tee e)
    ksh: syntax error: `>' unexpected

  6. Re: redirect stderr to two different files

    On Oct 16, 11:36*am, pk wrote:
    > On Thursday 16 October 2008 10:54, johnmmcparland wrote:
    >
    >
    >
    > > Hi all,

    >
    > > I wish to redirect stdout and stderr to the same file and also
    > > redirect stderr to a 2nd file. *E.g.

    >
    > > someprocess >outfile 2>&1

    >
    > > gets me both stdout and stderr into outfile but how do I get stderr
    > > into a second file, errfile as well as outfile?

    >
    > > Test program;

    >
    > > #include

    >
    > > int main()
    > > {
    > > * * fprintf(stdout, "standard output (1)\n");
    > > * * fprintf(stderr, "standard error (2)\n");
    > > * * return 0;
    > > }

    >
    > someprocess 2>&1 > outfile | tee -a outfile > errfile
    >
    > Although I'm not sure whether the order in which the above will print lines
    > inside outfile is deterministic.


    that seems to work. Hopefully the derministic nature won't affect the
    order too much as it is significant.

  7. Re: redirect stderr to two different files

    On Thursday 16 October 2008 12:36, pk wrote:

    > someprocess 2>&1 > outfile | tee -a outfile > errfile
    >
    > Although I'm not sure whether the order in which the above will print
    > lines inside outfile is deterministic.


    It's not :-)
    And actually from my tests it seems that sometimes some lines might be
    incomplete, so I guess that's not really a good solution.


  8. Re: redirect stderr to two different files

    On Thursday 16 October 2008 12:44, pk wrote:

    > On Thursday 16 October 2008 12:36, pk wrote:
    >
    >> someprocess 2>&1 > outfile | tee -a outfile > errfile


    This looks a bit better:

    someprocess 2>&1 >>outfile | tee -a outfile > errfile

    But still the ordering of lines is not deterministic.


  9. Re: redirect stderr to two different files

    2008-10-16, 12:44(+02), pk:
    > On Thursday 16 October 2008 12:36, pk wrote:
    >
    >> someprocess 2>&1 > outfile | tee -a outfile > errfile
    >>
    >> Although I'm not sure whether the order in which the above will print
    >> lines inside outfile is deterministic.

    >
    > It's not :-)
    > And actually from my tests it seems that sometimes some lines might be
    > incomplete, so I guess that's not really a good solution.


    You'd need >> outfile for the stdout not to overwrite the
    stderr.

    A slightly better approach, but you can't prevent the order to
    be affected anyway:

    { someprocess 2>&1 >&3 | tee errfile >&3; } 3> outfile

    --
    Stéphane

  10. Re: redirect stderr to two different files

    On Thursday 16 October 2008 12:47, Stephane CHAZELAS wrote:

    > 2008-10-16, 12:44(+02), pk:
    >> On Thursday 16 October 2008 12:36, pk wrote:
    >>
    >>> someprocess 2>&1 > outfile | tee -a outfile > errfile
    >>>
    >>> Although I'm not sure whether the order in which the above will print
    >>> lines inside outfile is deterministic.

    >>
    >> It's not :-)
    >> And actually from my tests it seems that sometimes some lines might be
    >> incomplete, so I guess that's not really a good solution.

    >
    > You'd need >> outfile for the stdout not to overwrite the
    > stderr.


    Yup, I discovered that.

    > A slightly better approach, but you can't prevent the order to
    > be affected anyway:
    >
    > { someprocess 2>&1 >&3 | tee errfile >&3; } 3> outfile


    That's better and also easier to understand (to me at least).
    Thanks!


+ Reply to Thread