[comp.unix.questions] Piping stderr in Korn and/or Bourne

carroll@m.cs.uiuc.edu (04/07/90)

/* Written  1:28 pm  Apr  5, 1990 by brad@SSD.CSD.HARRIS.COM in m.cs.uiuc.edu:comp.unix.questions */
/* ---------- "Piping stderr in Korn and/or Bourne" ---------- */
>I bow to any of you knowledgeable souls out there
>that can assist me with this problem ...
>
>I have a program that is supposed to read error output from other
>programs. I want to pipe stderr to this program WITHOUT piping
>stdin as well. I have not been able to find this in TFM (I have 
>been looking at pages 136-140 in Korn's book). 
> [ ... ]
>I was hoping one of these last two might give me the output:
>
>    this is stdout
>    this is STDerr
>
>I  would really like to be able to redirect only stderr to my program
>and to leave stdout untouched! (all in a single command). Can this be
>done in ksh? (sh? csh?)
/* End of text from m.cs.uiuc.edu:comp.unix.questions */
Here are two solutions I came up with:

tst 2>&1 >/dev/tty | sed ...
tst 3>&2 2>&1 >&3 | sed ...

Alan M. Carroll                Barbara/Marilyn in '92 :
carroll@cs.uiuc.edu            + This time, why not choose the better halves?
Epoch Development Team         + A Bush/Quayle ticket you can feel good about
CS Grad / U of Ill @ Urbana    ...{ucbvax,pur-ee,convex}!cs.uiuc.edu!carroll

brad@SSD.CSD.HARRIS.COM (Brad Appleton) (04/08/90)

Thank you to all who replied to my Korn-shell question on piping 
stderr but leaving stdout alone! Below is a summary of my e-mail
replies (you all saw the ones that were posted):

Contents of file "tst":

	#!/bin/ksh
	print "this is stdout"
	print -u2 "this is stderr"

Solutions:

Michael T. Sullivan, Alan M. Carrol, and Andy Behrens all suggested:

	$ tst 2>&1 >/dev/tty | sed 's/std/STD/'

This actually didnt work for me (probably to a slight system diff)
I got "ksh: cannot create device". A minor variation fixed this up:

	$ tst 2>&1 >`tty` | sed 's/std/STD/'
	this is stdout
	this is STDerr

Alan M. Carrol also suggested:

	$ tst 3>&2 2>&1 >&3 | sed 's/std/STD/'
	this is stdout
	this is STDerr

(The old swap method) Works like a charm, also has the added benefit
that it truly does leave stdout in the same place it was (tty or not).
I hereby declare it the winner (IMHO of course ;-)

Thank you all once again!
-=-=-=-=-=-=-=-=-=-=-= "And miles to go before I sleep." =-=-=-=-=-=-=-=-=-=-=-
 Brad Appleton         Harris Computer Systems         brad@ssd.csd.harris.com
 (305) 973-5007        Fort Lauderdale, FL USA        {uunet,novavax}!hcx1!brad
-=-=-=-=-=-=-=-=-=-= Disclaimer: I said it, not my company! -=-=-=-=-=-=-=-=-=-

brad@SSD.CSD.HARRIS.COM (Brad Appleton) (04/09/90)

In article <3660@hcx1.SSD.CSD.HARRIS.COM> brad@SSD.CSD.HARRIS.COM (Brad Appleton) writes:
>
>Alan M. Carrol also suggested:
>
>	$ tst 3>&2 2>&1 >&3 | sed 's/std/STD/'
>	this is stdout
>	this is STDerr
>
>(The old swap method) Works like a charm,

This is okay, the following is just plain wrong:

> also has the added benefit
>that it truly does leave stdout in the same place it was (tty or not).
>I hereby declare it the winner (IMHO of course ;-)
>
      ^                ^                 ^
      |                |                 |	
 Now this is utter rubbish (and just plain dumb of me)!!	
Of course stdout is not untouched here (it now points to 
wherever stderr previously pointed). Disregard my previous nonsense
(or all of my non-sense if you like :-)

Thank you all once again for your help!
-=-=-=-=-=-=-=-=-=-=-= "And miles to go before I sleep." =-=-=-=-=-=-=-=-=-=-=-
 Brad Appleton         Harris Computer Systems         brad@ssd.csd.harris.com
 (305) 973-5007        Fort Lauderdale, FL USA        {uunet,novavax}!hcx1!brad
-=-=-=-=-=-=-=-=-=-= Disclaimer: I said it, not my company! -=-=-=-=-=-=-=-=-=-