in reply to Redirecting STDOUT and standard error to file

Back in 1996, Tom Christiansen's celebrated "Csh Programming Considered Harmful" post showed how it can be done even in humble /bin/sh (but not in csh, of course ;-) as follows:

Consider the pipeline:
A | B | C
You want to know the status of C, well, that's easy: it's in $?, or $status in csh. But if you want it from A, you're out of luck -- if you're in the csh, that is. In the Bourne shell, you can get it, although doing so is a bit tricky. Here's something I had to do where I ran dd's stderr into a grep -v pipe to get rid of the records in/out noise, but had to return the dd's exit status, not the grep's:
device=/dev/rmt8 dd_noise='^[0-9]+\+[0-9]+ records (in|out)$' exec 3>&1 status=`((dd if=$device ibs=64k 2>&1 1>&3 3>&- 4>&-; echo $? >&4) +| egrep -v "$dd_noise" 1>&2 3>&- 4>&-) 4>&1` exit $status;

Update: Please note that the above block quoted text is a direct quote from Tom Christiansen's post.

Replies are listed 'Best First'.
Re^2: Redirecting STDOUT and standard error to file
by Anonymous Monk on Sep 09, 2005 at 14:25 UTC
    Given that the original poster asked about using "tee.exe", I assume he's running a Windows variant. Neither csh nor the bourne shell nor even dd is installed on Windows by default; and so it's not a great idea to assume that his default shell isn't just "command.com" in his case.

    Your points are all correct; just probably a bit too UNIX centric for the original poster to make use of. It's nice to want to help, but let's try to pick up on the subtle cues (operating system in use, etc.) that will make our advice that much more useful to the people who ask for it.

    The wiser we all try to be, the more powerful the monestary will become, until one day we TAKE OVER THE WORLD!!! :-)

      You are correct. I am running a windows XP/2000 machine and most of the solutions are Unix centric and I am not able to use them.
Re^2: Redirecting STDOUT and standard error to file
by welchavw (Pilgrim) on Sep 09, 2005 at 18:04 UTC
    Great post. I have one question you may wish to ponder - what the heck is going on with fd 3 in this example? As far as I can see, the manipulations of fd 3 are (as with dd's output in the example) just noise. I know its OT, but I wonder about this.