Subject: Re: Unixism, pipes and pkgsrc
To: Takahiro Kambe <taca@back-street.net>
From: Andrew Reilly <andrew-netbsd@areilly.bpc-users.org>
List: netbsd-users
Date: 08/23/2007 12:08:53
On Wed, Aug 22, 2007 at 10:08:34AM +0900, Takahiro Kambe wrote:
> In message <46CB8899.6010605@gmail.com>
> on Wed, 22 Aug 2007 02:51:37 +0200,
> Jan Danielsson <jan.m.danielsson@gmail.com> wrote:
> > Michael Parson wrote:
> > > It might help if you posted your script, or at least the sections of the
> > > script that are handling the redirection and your invocation of said
> > > script.
> >
> > cd /usr/pkgsrc/any/package
> > make package 2>&1 >> /tmp/build.log
> >
> > (Yes, I want to append to the build.log file)
> I think that you want do was:
>
> % make package >> /tmp/build.log 2>&1
Yes. Some of the previous suggestions have got the order wrong.
To the original person (Michael), the way to think about what is
happening is that the > (or >>) and the 2>&1 are two separate
file descriptor operations, and the shell processes them in
sequence, from left to right.
So
foo 2>&1 >foo-out.txt
runs foo after making stderr (fd 2) be the same as stdout (fd
1), which makes the error appear on the screen, and *then* opens
foo-out.txt for writing and assigns fd 1 (stdout) to that. This
second assignment does not affect where fd 2 is pointing, which
is still to the tty.
on the other hand,
foo >foo-out.txt 2>&1
runs foo after opening foo-out.txt as fd 1 (stdout) and *then*
makes fd 2 (stderr) a copy of fd 1, so both stdout and stderr go
to the file.
Just to throw another option into the mix, it is very common to
use the "script" command for this sort of purpose, because
that's precicely what it was created for... Script has the
(possible) advantage of also capturing stdin, should your
build process happen to go interactive at some stage.
Cheers,
--
Andrew