Advanced shell scripting question :-)

Steven W. Orr steveo at syslang.net
Thu Sep 5 10:04:17 EDT 2002


On Thu, 5 Sep 2002, Steven W. Orr wrote:

=>On Wed, 4 Sep 2002, Marc Evans wrote:
=>
=>=>How about something like this:
=>=>
=>=>	p 3>ERR 2>&3 2>&1 | tee OUTERR
=>
=>Interesting, but it doesn't quite work. This translates into english as:
=>"Run p and redirect channel 3 to the ERR file. Then dup channel 2 onto 
=>channel 3, and then dup channel 2 to channel 1. Next take the whole 
=>shebang and pipe it out to tee where it all goes to both."
=>
=>This doesn't actually work, and to be honest I didn't quite see why not,
=>but another person suggested that the solution might somehow use process
=>substitution. So I tinkered a bit and the final solution is:
=>
=>[drum roll please!]
=>
=>
=>p 3> >(tee ERR) 2>&1 2>&3 | tee BOTH

AUUGGGHHH!!! I spoke too soon!

What I currently have that gets me closer is as follows:

p  2>&1 2> >(tee ERR)  | tee BOTH

The order seems to make a difference and to not be intuitive. i.e., I 
would have expected that the following would be more correcter:

p 2> >(tee ERR)  2>&1 | tee BOTH

The problem is this: I expect that when data is written to a pipe, that 
the the order is preserved. Using this construct, the order is *not* 
preserved. So on a p that produces many kilobytes of spew, I get big 
chunks in the BOTH file which are from each seperate channel.

By the way, here's the p I'm using:

#! /bin/bash
while [[ 1 ]]
do
    echo "This is going to stdout"
    echo "This is going to stderr" 1>&2
done

Am I crazy or is order supposed to be preserved over pipes, or is that 
just a SYSV thing that I shouldn't expect under Linux (which is 
STREAMSless)?

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

-- 
-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? steveo at syslang.net




More information about the gnhlug-discuss mailing list