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 lines with sh in it
$ ps|(head -1; grep sh)
  PID  TT  STAT      TIME COMMAND

# print first and last two lines
$ look xa | (head -2; tail -2)
xanthaline
xanthamic

Not quite what you expected, right?

Yes, you can use read and echo N times but this is not
as convenient as using head:

$ look xa | (read a; echo $a; read a; echo $a; tail -2)
xanthaline
xanthamic
xarque
Xaverian

The "fix" is to make sure head reads no more than $N bytes
where $N is the number of *remaining* lines to be read.
Yes this slows head down some but makes it more useful.
[Ideally all commands that quit after partially reading
their input ought to behave like this but that would slow
down their common use far too much]

Comments?

Thanks to Rob Warnock for pointing out the head problem.
_______________________________________________
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"

Reply via email to