From owner-freebsd-hackers@FreeBSD.ORG Sun Jun 6 21:58:38 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 8DDB41065675 for ; Sun, 6 Jun 2010 21:58:38 +0000 (UTC) (envelope-from dougb@FreeBSD.org) Received: from mail2.fluidhosting.com (mx21.fluidhosting.com [204.14.89.4]) by mx1.freebsd.org (Postfix) with ESMTP id 3585B8FC0A for ; Sun, 6 Jun 2010 21:58:38 +0000 (UTC) Received: (qmail 4518 invoked by uid 399); 6 Jun 2010 21:58:37 -0000 Received: from localhost (HELO foreign.dougb.net) (dougb@dougbarton.us@127.0.0.1) by localhost with ESMTPAM; 6 Jun 2010 21:58:37 -0000 X-Originating-IP: 127.0.0.1 X-Sender: dougb@dougbarton.us Message-ID: <4C0C1A0B.4090409@FreeBSD.org> Date: Sun, 06 Jun 2010 14:58:35 -0700 From: Doug Barton Organization: http://SupersetSolutions.com/ User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.9.1.9) Gecko/20100330 Thunderbird/3.0.4 MIME-Version: 1.0 To: =?UTF-8?B?RGFnLUVybGluZyBTbcO4cmdyYXY=?= References: <20100605201242.C79345B52@mail.bitblocks.com> <4C0AB448.2040104@FreeBSD.org> <86r5kk6xju.fsf@ds4.des.no> In-Reply-To: <86r5kk6xju.fsf@ds4.des.no> X-Enigmail-Version: 1.0.1 OpenPGP: id=1A1ABC84 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Cc: freebsd-hackers@freebsd.org Subject: Re: head behaviour X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 06 Jun 2010 21:58:38 -0000 On 06/06/10 07:33, Dag-Erling Smørgrav wrote: > Doug Barton writes: >> Bakul Shah writes: >>> $ ps|(head -1; grep sh) >>> PID TT STAT TIME COMMAND >> I don't understand why you think this would work. There is no input to >> the grep command. The only reason it exits at all is that you are >> executing in a subshell. > > The output from ps is piped to the subshell, so all processes within > that subshell get their stdin from that output. > > Bakul's error is in assuming that head will *consume* only the first > line of input, whereas the only guarantee actually given is that it will > *print* only the first line of input. If you have a 2 line file named foo that looks like this: one two Then do: cat foo | (cat ; echo 'blah' ; cat) you get: one two blah which seems to indicate to me that unless the first command is a shell builtin that the first command is going to receive all of the stdin, and the second command none of it. In any case my point above is still accurate, there was no stdin for Bakul's grep command because head swallowed it. However, even if there are non-builtins that act differently the more basic point is still valid, this is not a technique that should be relied on without testing to make sure that it's going to do what you think it's going to do. :) Doug -- ... and that's just a little bit of history repeating. -- Propellerheads Improve the effectiveness of your Internet presence with a domain name makeover! http://SupersetSolutions.com/