Re: head behaviour

2010-06-08 Thread Dominic Fandrey
On 08/06/2010 00:59, Brian Somers wrote: On Mon, 07 Jun 2010 13:05:48 +0200, Dag-Erling Smørgrav d...@des.no wrote: Bakul Shah ba...@bitblocks.com writes: Except read doesn't do it quite right: $ ps | (read a; echo $a ; grep zsh) PID TT STAT TIME COMMAND yeah, I forgot that it drops

Re: head behaviour

2010-06-07 Thread Dag-Erling Smørgrav
Bakul Shah ba...@bitblocks.com writes: Except read doesn't do it quite right: $ ps | (read a; echo $a ; grep zsh) PID TT STAT TIME COMMAND yeah, I forgot that it drops leading whitespace... DES -- Dag-Erling Smørgrav - d...@des.no ___

Re: head behaviour

2010-06-07 Thread Brian Somers
On Mon, 07 Jun 2010 13:05:48 +0200, Dag-Erling Smørgrav d...@des.no wrote: Bakul Shah ba...@bitblocks.com writes: Except read doesn't do it quite right: $ ps | (read a; echo $a ; grep zsh) PID TT STAT TIME COMMAND yeah, I forgot that it drops leading whitespace... Well, leading

Re: head behaviour

2010-06-06 Thread Dag-Erling Smørgrav
Doug Barton do...@freebsd.org writes: Bakul Shah ba...@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

Re: head behaviour

2010-06-06 Thread Doug Barton
On 06/06/10 07:33, Dag-Erling Smørgrav wrote: Doug Bartondo...@freebsd.org writes: Bakul Shahba...@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

Re: head behaviour

2010-06-06 Thread Dag-Erling Smørgrav
Doug Barton do...@freebsd.org writes: 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

Re: head behaviour

2010-06-06 Thread Doug Barton
On 06/06/10 15:13, Dag-Erling Smørgrav wrote: The second command will receive whatever is left after the first is done. Otherwise, read(1) loops wouldn't work. You chose a poor example, since cat(1) consumes*everything*. Fair enough. My point remains though, using this technique is liable to

Re: head behaviour

2010-06-06 Thread Bakul Shah
On Mon, 07 Jun 2010 00:13:28 +0200 =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= d...@des.no wrote: The reason why head(1) doesn't work as expected is that it uses buffered I/O with a fairly large buffer, so it consumes more than it needs. The only way to make it behave as the OP expected is to use

Re: head behaviour

2010-06-06 Thread Rob Warnock
Bakul Shah ba...@bitblocks.com wrote: +-- | [cc'ed Rob in case he wishes to chime in] +- No, I think you covered most of it quite well [including the bit about grep -m not working properly in the case of pipes, sockets, special files, etc.], thanks. Yes, I know that grep(1) only

head behaviour

2010-06-05 Thread Bakul Shah
Consider: $ yes | cat -n | (read a; echo $a; head -1) 1 y 2 y $ yes | cat -n | (head -1; read a; echo $a) 1 y 456 y As you can see, head reads far more than it should. This is fine most of the time but often it results in surprising output: # print ps header and all

Re: head behaviour

2010-06-05 Thread Doug Barton
On 06/05/10 13:12, Bakul Shah wrote: Consider: $ yes | cat -n | (read a; echo $a; head -1) 1 y 2 y $ yes | cat -n | (head -1; read a; echo $a) 1 y 456 y It's not at all clear to me what you are trying to accomplish here. If what you want is to read only the first line

Re: head behaviour

2010-06-05 Thread Bakul Shah
On Sat, 05 Jun 2010 13:32:08 PDT Doug Barton do...@freebsd.org wrote: On 06/05/10 13:12, Bakul Shah wrote: Consider: $ yes | cat -n | (read a; echo $a; head -1) 1 y 2 y $ yes | cat -n | (head -1; read a; echo $a) 1 y 456 y It's not at all clear to me

Re: head behaviour

2010-06-05 Thread Doug Barton
On 06/05/10 13:48, Bakul Shah wrote: Without running the following can you guess its output? $ look '' | (head -2; head -2) Again, it's not clear to me what you expect is going to happen with the second 'head -2' there. I agree that the actual output of your example is wacky and unexpected,

Re: head behaviour

2010-06-05 Thread Bakul Shah
it needs to]. It would be less surprising and more useful if $ ps | (head -1; grep ssh) showed PID TT STAT TIME COMMAND all line with ssh in it The change in head behaviour I am suggesting wouldn't break anything that already works but make it more useful for what you call

Re: head behaviour

2010-06-05 Thread Bakul Shah
On Sat, 05 Jun 2010 17:02:42 EDT Mike Meyer m...@mired.org wrote: As a general rule, programs don't expect to share their input with other programs, nor do they make guarantees about what is and isn't read from that input under those conditions. I'd say that shell scripts that depend on what

Re: head behaviour

2010-06-05 Thread Dominic Fandrey
On 05/06/2010 23:37, Bakul Shah wrote: On Sat, 05 Jun 2010 14:02:16 PDT Doug Barton do...@freebsd.org wrote: It would be less surprising and more useful if $ ps | (head -1; grep ssh) showed PID TT STAT TIME COMMAND all line with ssh in it The change in head