*BSD News Article 28869


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!munnari.oz.au!spool.mu.edu!howland.reston.ans.net!pipex!sunic!EU.net!Germany.EU.net!netmbx.de!zib-berlin.de!news.th-darmstadt.de!iti.informatik.th-darmstadt.de!deeken
From: deeken@iti.informatik.th-darmstadt.de (Hannes Deeken)
Newsgroups: comp.os.386bsd.bugs
Subject: Re: yet more FreeBSD-1.1-BETA install bugs
Date: 31 Mar 1994 14:37:21 +0200
Organization: TU Darmstadt, ITI
Lines: 48
Message-ID: <deeken.765102160@iti.informatik.th-darmstadt.de>
References: <Cn7vwo.2z0@cosy.sbg.ac.at> <JKH.94Mar25151624@sentnl.ilo.dec.com>  <2n71jn$6c3@Germany.EU.net> <JKH.94Mar29161752@sentnl.ilo.dec.com> <deeken.764955732@iti.informatik.th-darmstadt.de> <2nc8th$8mn@Germany.EU.net>
NNTP-Posting-Host: spelunke.iti.informatik.th-darmstadt.de

bs@Germany.EU.net (Bernard Steiner) writes:

>In article <deeken.764955732@iti.informatik.th-darmstadt.de>, deeken@iti.informatik.th-darmstadt.de (Hannes Deeken) writes:
>|> Because '>/dev/null 2>&1' redirects stdout _and_ stderr to /dev/null,
>|> so 'BLAH' gets no input. Bernards form works, but IMHO '2>&1 >/dev/null'
>|> is more suitable, since you don't get another subprocess.

>Are you sure that will give you stderr dup'ed to fd1 so it can be piped to the
>magic stuff ? I seem to recall vaguely that I tried it once but it didn't
>work. Quite understandably not, since dup2() doesn't care whether it is told
>to put fd2 on fd1 when fd1 is assciated with the tty or with /dev/null.

I'm quite sure :) I tried it with bash and with /bin/sh on NetBSD 0.9.

It's not a matter what dup2 cares, it's a matter of which file a descriptor
is associated with at a given time.

The form '>/dev/null 2>&1' has the effect of

 	newfd = open("/dev/null", ...);
	dup2(newfd, 1);	    /* nearly the same as close(1); dup(newfd); */
	close(newfd);
	dup2(1, 2);	    /* nearly the same as close(2); dup(1);     */

This way, both file descriptors get associated with /dev/null.

If you use '2>&1 >/dev/null', you get

	dup2(1, 2);		/* == close(2); dup(1);     */
	newfd = open("/dev/null", ...);
	dup2(newfd, 1);	    /* nearly the same as close(2); dup(1);     */
	close(newfd);

So, fd 2 stays associated with whatever fd 1 was associated before
and fd 1 breaks up its former association and gets associated
with /dev/null. If fd 1 was associated with a pipe, fd 2 is now associated
with that pipe.

I didn't look at the source, but (at least for me) it works like this,
and it makes sense to me ;-)


Hannes
-- 
Hans-Christoph Deeken | hannes@flinx.{RoBIN.de,hotb.sub.org} (home)
Jungfernstrasse 34    | deeken@iti.informatik.th-darmstadt.de (university)
64291 Darmstadt       | IRC: Glenlivet