On Wed, 4 Sep 2002, Steven W. Orr wrote:

=>I have a program (which we shall call p) which produces text to both 
=>stdout and stderr.
=>
=>I want the following three things to happen when I run p:
=>
=>1. I want both stdout and stderr to go to the screen
=>
=>2. I want stdout and stderr combined in a file
=>
=>3. I want an error log file to only contain stderr.
=>
=>p 2>&1 | tee outnerr # Solves 1 and 2
=>
=>p 2> errlog   # Solves 3 but breaks 1 and 2
=>
=>p 2> errlog | tee out # Solves 3 but also breaks 1 and 2.
=>
=>Any takers?
All kinds of fun games have been played, but I now have
<GermanAccent>Ze Final Solution</GermanAccent>.

{
    p 2>&1 >&3 3>&- | tee err
} 3>&1 | tee out

[This was so elegant I almost cried when I saw it.]

But! This still suffers slightly from the buffering problem we were 
looking at that also turned out to be part of the problem. To fix that, I 
took a look at the unbuffer command which comes with the expect package. I 
modified it so it would not do the unbuffering that it was designed for. 
Instead, it now does straight line buffering.

(The line buffering was turned off by virtue of running through a pipe.)

Here is the new lbuffer command:

#!/usr/bin/expect --
eval spawn $argv
set timeout -1
expect

So the total solution is this:

{
    lbuffer p 2>&1 >&3 3>&- | tee err
} 3>&1 | tee out

Thanks to everyone :-)

You may now talk amongst yourselves.

-- 
-Time flies like the wind. Fruit flies like a banana. Stranger things have -
-happened but none stranger than this. Does your driver's license say Organ
-Donor?Black holes are where God divided by zero. Listen to me! We are all-
-individuals! What if this weren't a hypothetical question? [EMAIL PROTECTED]

_______________________________________________
gnhlug-discuss mailing list
[EMAIL PROTECTED]
http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss

Reply via email to