Date: Sun, 06 Jun 2010 16:33:09 +0200 From: =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= <des@des.no> To: Doug Barton <dougb@FreeBSD.org> Cc: freebsd-hackers@freebsd.org Subject: Re: head behaviour Message-ID: <86r5kk6xju.fsf@ds4.des.no> In-Reply-To: <4C0AB448.2040104@FreeBSD.org> (Doug Barton's message of "Sat, 05 Jun 2010 13:32:08 -0700") References: <20100605201242.C79345B52@mail.bitblocks.com> <4C0AB448.2040104@FreeBSD.org>
next in thread | previous in thread | raw e-mail | index | archive | help
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. This particular one-liner can be implemented reliably as follows: % ps | (read header; echo $header; grep sh) PID TT STAT TIME COMMAND 3415 0 Ss 0:00.31 -zsh (zsh) 3476 0 S+ 0:00.00 -zsh (zsh) because read is a shell built-in, and the shell guarantees that it will not consume more input than necessary. DES --=20 Dag-Erling Sm=C3=B8rgrav - des@des.no
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?86r5kk6xju.fsf>