[svlug] bash i/o redirect: stdout & stderr to file *AND* stderr to tty?

kmself@ix.netcom.com kmself at ix.netcom.com
Wed May 17 11:53:09 PDT 2000


My (already questionable) reputation is at stake.

A friend has the following challenge, and a Perl solution:

    Execute a command.
    Log both stdout and stderr to a file.
    Also write stderr to output.

The Perl method is:

    #! /usr/bin/perl
    use IPC::Open3;
    open3("<&STDIN", ">&STDOUT", \*ERR, @ARGV);
    select(STDERR);
    $| = 1;
    select(STDOUT);
    $| = 1;
    while (<ERR>) {
    print;
    print STDERR;
    }

The best I can come up with is:

    >log; foo 2>&1 3>>log 1>&3 | tee -a log

...which works, but doesn't guarantee output sequence.  Eg, with a
simple script:

    #!/usr/bin/env bash
    echo "stdout 1"
    echo "stderr 1" 1>&2
    echo "stdout 2"
    echo "stderr 2" 1>&2

...the contents of log might be either A:

    stdout 1
    stdout 2
    stderr 1
    stderr 2

or B:

    stdout 1
    stderr 1
    stdout 2
    stderr 2

Does anyone know a shell trick that *will* guarantee that the sequence
is always as B:, for an arbitrary command?

TIA.

-- 
Karsten M. Self <kmself at ix.netcom.com>         http:/www.netcom.com/~kmself
    What part of "Gestalt" don't you understand?
    http://gestalt-system.sourceforge.net/
GPG fingerprint: F932 8B25 5FDD 2528 D595  DC61 3847 889F 55F2 B9B0
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
Url : http://lists.svlug.org/archives/svlug/attachments/20000517/94444165/attachment.bin


More information about the svlug mailing list