Thread View: comp.unix.shell
13 messages
13 total messages
Started by Pim Zandbergen
Mon, 08 Oct 1990 16:51
How to pipe stderr to a command in Bourne or Korn shell
Author: Pim Zandbergen
Date: Mon, 08 Oct 1990 16:51
Date: Mon, 08 Oct 1990 16:51
7 lines
703 bytes
703 bytes
How can one redirect stderr to a command while leaving stdout unaffected ? -- Pim Zandbergen domain : pim@cti-software.nl CTI Software BV uucp : uunet!mcsun!hp4nl!ctisbv!pim Laan Copes van Cattenburch 70 phone : +31 70 3542302 2585 GD The Hague, The Netherlands fax : +31 70 3512837
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Richard Coleman
Date: Mon, 08 Oct 1990 19:32
Date: Mon, 08 Oct 1990 19:32
15 lines
995 bytes
995 bytes
--
In article <1990Oct8.165133.17187@cti-software.nl>, pim@cti-software.nl (Pim Zandbergen) writes:
|>
|> How can one redirect stderr to a command while leaving stdout unaffected ?
For csh and bash try (command > temp1) >& temp2
For sh command 2> temp
somebody else will have to handle ksh.
Richard Coleman
G.E. Simulation & Control Systems
coleman@sunny.dab.ge.com
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Jonathan I. Kamens
Date: Mon, 08 Oct 1990 20:40
Date: Mon, 08 Oct 1990 20:40
25 lines
1661 bytes
1661 bytes
In article <6133@ge-dab.GE.COM>, coleman@sunny.DAB.GE.COM (Richard Coleman) writes:
|> In article <1990Oct8.165133.17187@cti-software.nl>, pim@cti-software.nl (Pim Zandbergen) writes:
|> |> How can one redirect stderr to a command while leaving stdout unaffected ?
|> For csh and bash try (command > temp1) >& temp2
|> For sh command 2> temp
The original poster asked how to redirect to a *command*. You've shown how
to redirect to a *file*.
In csh, one possible way to do it (when working at a tty) would be:
(program > /dev/tty) |& command
A similar approach will work in sh (and probably ksh), although there's
probably some better way to do it with various hideous file descriptor
reassignments (I don't use the bourne shell a lot, so I don't qualify to
invent hideous file descriptor reassignments :-):
(program > /dev/tty) 2>&1 | command
--
Jonathan Kamens USnail:
MIT Project Athena 11 Ashford Terrace
jik@Athena.MIT.EDU Allston, MA 02134
Office: 617-253-8495 Home: 617-782-0710
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Paul John Falstad
Date: Mon, 08 Oct 1990 21:12
Date: Mon, 08 Oct 1990 21:12
18 lines
1310 bytes
1310 bytes
In article <1990Oct8.204053.15797@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes:
>|> |> How can one redirect stderr to a command while leaving stdout unaffected ?
>A similar approach will work in sh (and probably ksh), although there's
>probably some better way to do it with various hideous file descriptor
>reassignments (I don't use the bourne shell a lot, so I don't qualify to
>invent hideous file descriptor reassignments :-):
>
> (program > /dev/tty) 2>&1 | command
They're not that hideous!
In bash, sh, and (I'm fairly sure) ksh, you can do this and avoid the
subshell:
program 2>&1 >/dev/tty | command
--
Are you nervy? Irritable? Depressed? Tired of life? Keep it up!
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Conor P. Cahill
Date: Mon, 08 Oct 1990 23:32
Date: Mon, 08 Oct 1990 23:32
13 lines
1054 bytes
1054 bytes
In article <1990Oct8.165133.17187@cti-software.nl> pim@cti-software.nl (Pim Zandbergen) writes:
>
>How can one redirect stderr to a command while leaving stdout unaffected ?
Assuming you are asking this because you are using the c-shell (that doesn't
support 2>) I will point you toward the comp.unix.questions FAQ posting
(just posted in the last week) question number 16.
--
Conor P. Cahill (703)430-9247 Virtual Technologies, Inc.,
uunet!virtech!cpcahil 46030 Manekin Plaza, Suite 160
Sterling, VA 22170
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Bob McGowen x4312 dept208
Date: Mon, 08 Oct 1990 23:41
Date: Mon, 08 Oct 1990 23:41
26 lines
1406 bytes
1406 bytes
In article <6133@ge-dab.GE.COM> coleman@sunny.DAB.GE.COM (Richard Coleman) writes: > >-- >In article <1990Oct8.165133.17187@cti-software.nl>, pim@cti-software.nl (Pim Zandbergen) writes: >|> >|> How can one redirect stderr to a command while leaving stdout unaffected ? ^^^^^^^^ ^^^^^^^^^^^^ > > >For csh and bash try (command > temp1) >& temp2 >For sh command 2> temp > >somebody else will have to handle ksh. > The question was how to redirect to a command, not a file. You might think that something like 2| would work, but it doesn't (at least in sh, I have no ksh to try it on). I have tried several permutations of re-ordering file descriptors and trying to use new ones with the _exec- builtin but have had no success yet. Bob McGowan (standard disclaimer, these are my own ...) Product Support, Wyse Technology, San Jose, CA ..!uunet!wyse!bob bob@wyse.com
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Dan Bernstein
Date: Tue, 09 Oct 1990 03:10
Date: Tue, 09 Oct 1990 03:10
22 lines
1459 bytes
1459 bytes
In article <1990Oct8.204053.15797@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes: > In csh, one possible way to do it (when working at a tty) would be: But who said there's a tty around? What if Pim wanted to pipe his output through yet another command? > A similar approach will work in sh (and probably ksh), although there's > probably some better way to do it with various hideous file descriptor > reassignments (I don't use the bourne shell a lot, so I don't qualify to > invent hideous file descriptor reassignments :-): I use the bourne shell a lot, so I guess I qualify. It isn't too hideous, though, if you write it clearly: ( exec 5>&1; exec 1>&2; exec 2>&5; foo ) | err-processor Even you :-) should be able to recognize the swap idiom there. Start from basic constructs like this and soon you'll be piping stdout and stderr through separate commands. Someday you may even learn to use multitee... [grin] ---Dan
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Henk M. Keller
Date: Tue, 09 Oct 1990 09:46
Date: Tue, 09 Oct 1990 09:46
35 lines
2132 bytes
2132 bytes
In article <1990Oct8.165133.17187@cti-software.nl>, pim@cti-software.nl (Pim Zandbergen) writes: > > How can one redirect stderr to a command while leaving stdout unaffected ? The real solution in sh and ksh is to exchange stdout and stderr: command_1 3>&1 1>&2 2>&3 3>&- | command_2 Well, some comments may be needed :-) (a) 3>&1 File descriptor 3 now is an 'alias' for 1 (original stdout) (b) 1>&2 File descr. 1 now is an 'alias' for 2 (original stderr) (c) 2>&3 File descr. 2 now is an 'alias' for 3 which still is an alias for the original stdout (d) 3>&- File descr. 3 is closed as it is no longer needed Net effect: File descriptor 1 (stdout) is connected to the original stderr (b) File descriptor 2 (stderr) is connected to the original stdout (c) For C writers amongst you: 3>&1 will probably be implemented as close(3); /* To ensure 3 is free */ fcntl(1, F_DUPFD, 3); /* Returns lowest possible */ /* 'dup'ed file descr. >= 3 */ /-------------------------------------------------------+ / Henk Keller AT Computing bv | | Teleph.: +31 80 566880 P.O. Box 1428 | | Fax: +31 80 555887 6501 BK Nijmegen | | Email: henk@atcmp.nl The Netherlands | | mcsun!hp4nl!kunivv1!atcmpe!henk | | | | -- do not fold -- | +---------------------------------------------------------+
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Rick Jones
Date: Tue, 09 Oct 1990 10:05
Date: Tue, 09 Oct 1990 10:05
21 lines
1453 bytes
1453 bytes
In article <3207@idunno.Princeton.EDU> pfalstad@flower.Princeton.EDU (Paul John Falstad) writes: >In article <1990Oct8.204053.15797@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes: >>|> |> How can one redirect stderr to a command while leaving stdout unaffected ? > >In bash, sh, and (I'm fairly sure) ksh, you can do this and avoid the >subshell: > >program 2>&1 >/dev/tty | command In sh, file descriptor re-directions are evaluated left to right, so you can even swap stdout & stderr with a little care & avoid /dev/tty if you want: program 3>&2 2>&1 >&3 | command I think this should work in ksh, but doesn't seem to in my version - a buglet? I can't speak for bash as I don't use it. -- Rick Jones The definition of atomic: Tetra Ltd. from the Greek meaning "indivisible" Maidenhead, Berks, UK So what is: rick@tetrauk.uucp an atomic explosion?
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Maarten Litmaath
Date: Tue, 09 Oct 1990 17:59
Date: Tue, 09 Oct 1990 17:59
28 lines
1415 bytes
1415 bytes
In article <668@atcmpe.atcmp.nl>,
henk@atcmp.nl (Henk M. Keller) writes:
)In article <1990Oct8.165133.17187@cti-software.nl>,
) pim@cti-software.nl (Pim Zandbergen) writes:
)>
)> How can one redirect stderr to a command while leaving stdout unaffected ?
)
)The real solution in sh and ksh is to exchange stdout and stderr:
)
) command_1 3>&1 1>&2 2>&3 3>&- | command_2
But that still does _not_ answer Pim's question!
He said: `... while leaving stdout _unaffected_' (emphasis added).
In Henk's example stdout is dup(2)ed to stderr.
So:
exec 3>&1
command_1 2>&1 1>&3 3>&- | command_2 3>&-
exec 3>&-
Or:
3>&1 (command_1 2>&1 1>&3 3>&- | command_2 3>&-)
# yes Virginia, that's correct
Don't forget to close the extra descriptor, or you'll get screwed one day.
--
"the C shell is flakier than a snowstorm." (Guy Harris)
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Dan Bernstein
Date: Tue, 09 Oct 1990 20:26
Date: Tue, 09 Oct 1990 20:26
9 lines
722 bytes
722 bytes
In article <668@atcmpe.atcmp.nl> henk@atcmp.nl (Henk M. Keller) writes: > 3>&1 will probably be implemented as > close(3); /* To ensure 3 is free */ > fcntl(1, F_DUPFD, 3); /* Returns lowest possible */ > /* 'dup'ed file descr. >= 3 */ Or dup2(1,3). ---Dan
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Paul Chamberlain
Date: Tue, 09 Oct 1990 22:23
Date: Tue, 09 Oct 1990 22:23
23 lines
1564 bytes
1564 bytes
In article <3002@wyse.wyse.com> bob@wyse.UUCP (Bob McGowen x4312 dept208) writes: >In article <1990Oct8.165133.17187@cti-software.nl>, pim@cti-software.nl writes: >> How can one redirect stderr to a command while leaving stdout unaffected ? >The question was how to redirect to a command, not a file. You might think >that something like 2| would work, but it doesn't (at least in sh, I have >no ksh to try it on). > >I have tried several permutations of re-ordering file descriptors and trying >to use new ones with the _exec- builtin but have had no success yet. Just thought I'd take a stab at it. There may be a better way. I use Korn Shell. $ echo now is the time > xx $ rm -f zz $ ( cat zz xx 2>&1 1>&3 | od -c ) 3>&1 now is the time 0000000 c a t : c a n n o t o p e n 0000020 z z \n 0000024 Paul Chamberlain | I do NOT represent IBM. tif@doorstop, sc30661 at ausvm6 512/838-7008 | ...!cs.utexas.edu!ibmaus!auschs!doorstop.austin.ibm.com!tif
Re: How to pipe stderr to a command in Bourne or Korn shell
Author: Mark A. Emanuele
Date: Sat, 13 Oct 1990 21:02
Date: Sat, 13 Oct 1990 21:02
26 lines
1161 bytes
1161 bytes
In article <1990Oct8.165133.17187@cti-software.nl>, pim@cti-software.nl (Pim Zandbergen) writes: > > How can one redirect stderr to a command while leaving stdout unaffected ? I'm not 100% sure but logically this should work. command1 2>tempfile & tail -f tempfile | command2 the only problem I think would be that the tail -f would never end. another solution: again I'm not sure but here goes: command1 2|command2 I have not tried either of these . let me know if they work. -- Mark A. Emanuele V.P. Engineering Overleaf, Inc. 500 Route 10 Ledgewood, NJ 07852-9639 attmail!overlf!emanuele (201) 927-3785 Voice (201) 927-5781 fax emanuele@overlf.UUCP
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads