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

Tin Le tin at le.org
Wed May 17 14:27:23 PDT 2000


-----BEGIN PGP SIGNED MESSAGE-----

The short answer is no.  The problem you are encountering is stdio buffering
in bash.  The Perl script has to turn off buffering to get the output
ordering right.

- From looking through bash man pages, there is no flags or internal command
to turn off stdio buffering.

Tin Le

- ----
http://tin.le.org
Internet Security and Firewall Consulting
Tin Le - tin at le.org

>     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;

Turns off buffering here.

>     select(STDOUT);
>     $| = 1;

Turns off buffering here.

>     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?



-----BEGIN PGP SIGNATURE-----
Version: 2.6.2i

iQCVAgUBOSMOuBiIIbPkDHhBAQGkOwQAjatsuTW8HAqojwHLlZVVbwzDqASDBsZt
sVmtaujah/uhyUl5l+xE9pm6XUGcHNj9/PZ71zgVdil+kcjm18lASa2xFUPklbAD
HfXNpypTWgHOQ7S9JHTj1ls0Kl0Zp12+MQLeItZHz+7CYn/D0MaTJ8E6U7cQ2Tkh
4PgRtHjs+d0=
=XS+j
-----END PGP SIGNATURE-----






More information about the svlug mailing list