head behaviour
Doug Barton
dougb at FreeBSD.org
Sun Jun 6 21:58:38 UTC 2010
On 06/06/10 07:33, Dag-Erling Smørgrav wrote:
> Doug Barton<dougb at FreeBSD.org> writes:
>> Bakul Shah<bakul at 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/
More information about the freebsd-hackers
mailing list