9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] rc output redirection
@ 2009-04-02 16:56 hugo rivera
  2009-04-02 17:28 ` lucio
  2009-04-03  5:13 ` Russ Cox
  0 siblings, 2 replies; 4+ messages in thread
From: hugo rivera @ 2009-04-02 16:56 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Hi,
I got confused by output redirection, even though I have used it like
a thousand times:
cmd1 | cmd2 | cmd3
this pipes cmd1 stdout to cmd2's stdin and finally cmd2's stdout is
piped to cmd3's stdin. All cmd[1-3] stderr are displayed (presumably)
at the terminal along with cmd3's stdout, right?
And when you do
cmd1 |[2] cmd2 | cmd3
you get cmd1's stderr piped to cmd2's stdin and cmd2's stdout piped to
cmd3 stdin. cmd1's stdout along with cmd2's stderr and cmd3's stdout
and stderr go to the terminal.
But when you do something like
cmd1 | cmd2 |[2] cmd3
you get cmd1's stdout piped to cmd2's stdin; but my confusion begins
here: is it cmd1's or cmd2's stderr that gets redirected to cmd3's
stdin? maybe both? my guess is that cmd2's stderr is the one who gets
piped to cmd3's stdin and cmd1's stderr goes to the terminal. If this
is the case, how can I redirect cmd1's stderr to cmd3's stdin instead
of cmd2's? or both?
It's easy to think of more complex cases.
Saludos

--
Hugo



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9fans] rc output redirection
  2009-04-02 16:56 [9fans] rc output redirection hugo rivera
@ 2009-04-02 17:28 ` lucio
  2009-04-03  5:13 ` Russ Cox
  1 sibling, 0 replies; 4+ messages in thread
From: lucio @ 2009-04-02 17:28 UTC (permalink / raw)
  To: 9fans

> If this
> is the case, how can I redirect cmd1's stderr to cmd3's stdin instead
> of cmd2's? or both?

It's a pipe_line_, not a directed graph.  You could write a wrapper to
do what you want quite easily, but expecting a simple, generic shell
notation to do it is asking a bit much.  At least, I don't think I'd
try to wrap my brain around this, I'd write a ten line C program for
the specific requirement.

++L




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9fans] rc output redirection
  2009-04-02 16:56 [9fans] rc output redirection hugo rivera
  2009-04-02 17:28 ` lucio
@ 2009-04-03  5:13 ` Russ Cox
  2009-04-03  8:51   ` hugo rivera
  1 sibling, 1 reply; 4+ messages in thread
From: Russ Cox @ 2009-04-03  5:13 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> But when you do something like
> cmd1 | cmd2 |[2] cmd3
> you get cmd1's stdout piped to cmd2's stdin; but my confusion begins
> here: is it cmd1's or cmd2's stderr that gets redirected to cmd3's
> stdin? maybe both? my guess is that ...

why guess?

% {echo cmd1 >[1=2]} | {echo cmd2 >[1=2]} | sed 's/^/cmd3: /'
cmd1
cmd2
% {echo cmd1 >[1=2]} | {echo cmd2 >[1=2]} |[2] sed 's/^/cmd3: /'
cmd3: cmd1
cmd3: cmd2
%

not what i expected, but there you have it.
and it makes sense: cmd1 and cmd2 are both
writing to the same stderr, so redirecting it
redirects both.

> how can I redirect cmd1's stderr to cmd3's stdin instead
> of cmd2's? or both?

if you don't care about cmd2's stderr, then you can get rid of it

% {echo cmd1 >[1=2]} | {echo cmd2 >[2]/dev/null} |[2] sed 's/^/cmd3: /'
cmd2
cmd3: cmd1
%

> It's easy to think of more complex cases.

it's easy to think of elephants.

rc does its general-purpose job very well.
if you have special-purpose needs,
as lucio suggests, write a c program.

russ


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9fans] rc output redirection
  2009-04-03  5:13 ` Russ Cox
@ 2009-04-03  8:51   ` hugo rivera
  0 siblings, 0 replies; 4+ messages in thread
From: hugo rivera @ 2009-04-03  8:51 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

2009/4/3, Russ Cox <rsc@swtch.com>:
> > But when you do something like
>  > cmd1 | cmd2 |[2] cmd3
>  > you get cmd1's stdout piped to cmd2's stdin; but my confusion begins
>  > here: is it cmd1's or cmd2's stderr that gets redirected to cmd3's
>
> > stdin? maybe both? my guess is that ...
>
>  why guess?
>
>  % {echo cmd1 >[1=2]} | {echo cmd2 >[1=2]} | sed 's/^/cmd3: /'
>  cmd1
>  cmd2
>  % {echo cmd1 >[1=2]} | {echo cmd2 >[1=2]} |[2] sed 's/^/cmd3: /'
>  cmd3: cmd1
>  cmd3: cmd2
>  %
>
>  not what i expected, but there you have it.
>  and it makes sense: cmd1 and cmd2 are both
>  writing to the same stderr, so redirecting it
>  redirects both.
>
>
>  > how can I redirect cmd1's stderr to cmd3's stdin instead
>  > of cmd2's? or both?
>
>
> if you don't care about cmd2's stderr, then you can get rid of it
>
>  % {echo cmd1 >[1=2]} | {echo cmd2 >[2]/dev/null} |[2] sed 's/^/cmd3: /'
>  cmd2
>  cmd3: cmd1
>
> %
>
>  > It's easy to think of more complex cases.
>
>
> it's easy to think of elephants.
>
>  rc does its general-purpose job very well.
>  if you have special-purpose needs,
>  as lucio suggests, write a c program.
>
>
>  russ
>
>

ok, thanks

--
Hugo



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-04-03  8:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-02 16:56 [9fans] rc output redirection hugo rivera
2009-04-02 17:28 ` lucio
2009-04-03  5:13 ` Russ Cox
2009-04-03  8:51   ` hugo rivera

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).