Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion piping stdout and stderr to different processes?

Path: g2news2.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!ndsoftware.com!194.168.222.133.MISMATCH!news-out.ntli.net!newsrout1-gui.ntli.net!ntli.net!news.highwinds-media.com!newspeer1-win.ntli.net!newsfe1-win.ntli.net.POSTED!53ab2750!not-for-mail
Newsgroups: comp.unix.shell
From: Stephane CHAZELAS <this.addr...@is.invalid>
Subject: Re: piping stdout and stderr to different processes?
References: <slrnfacq0c.eed.njc@cookie.uucp> <slrnfae5u2.6ee.stephane.chazelas@spam.is.invalid> <slrnfaijgi.tvv.njc@cookie.uucp>
Mail-Copies-To: nobody
Message-ID: <slrnfajdgk.7af.stephane.chazelas@spam.is.invalid>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
User-Agent: slrn/0.9.8.1pl1 (Debian)
Lines: 43
Date: Fri, 27 Jul 2007 09:08:23 GMT
NNTP-Posting-Host: 82.26.171.2
X-Complaints-To: http://netreport.virginmedia.com
X-Trace: newsfe1-win.ntli.net 1185527303 82.26.171.2 (Fri, 27 Jul 2007 10:08:23 BST)
NNTP-Posting-Date: Fri, 27 Jul 2007 10:08:23 BST
Organization: Virgin Net Usenet Service

2007-07-26, 20:44(-05), Neil Cherry:
[...]
>> {
>>   {
>>     cm1 3>&- |
>>       cmd2 2>&3 3>&-
>>   } 2>&1 >&4 4>&- |
>>     cmd3 3>&- 4>&-
>> } 3>&2 4>&1
>
> Thanks.
>
> OK now I see why I didn't get it to work. I didn't try that far.
> But I have to say I'm not quite sure what I'm reading just yet.
> I'll have to hit the man pages as I'm not used to the >&-
> syntax.

3>&- is for closing fd 3. It's not necessary, but it's for tidy
up. None of the commands will ever try (not should they) to
access the fd 3 and 4, so it's best to close them before
executing those commands so that they can use those fds for
something else. 

{
  {
    cm1 |
      cmd2 2>&3
  } 2>&1 >&4 |
    cmd3
} 3>&2 4>&1

is functionnaly equivalent.

if cmd2 doesn't output anything on its stdout nor stderr, it can
even be simplified to:
{ cm1 | cmd2; } 2>&1 | cmd3

Or if you want to be sure:

{ cm1 | cmd2 > /dev/null 2>&1; } 2>&1 | cmd3

-- 
Stéphane