Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 06 Jun 2010 14:58:35 -0700
From:      Doug Barton <dougb@FreeBSD.org>
To:        =?UTF-8?B?RGFnLUVybGluZyBTbcO4cmdyYXY=?= <des@des.no>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: head behaviour
Message-ID:  <4C0C1A0B.4090409@FreeBSD.org>
In-Reply-To: <86r5kk6xju.fsf@ds4.des.no>
References:  <20100605201242.C79345B52@mail.bitblocks.com>	<4C0AB448.2040104@FreeBSD.org> <86r5kk6xju.fsf@ds4.des.no>

next in thread | previous in thread | raw e-mail | index | archive | help
On 06/06/10 07:33, Dag-Erling Smørgrav wrote:
> Doug Barton<dougb@FreeBSD.org>  writes:
>> Bakul Shah<bakul@bitblocks.com>  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
<prompt>

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/




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4C0C1A0B.4090409>