Berkeley 'head' script

rwl at uvacs.UUCP rwl at uvacs.UUCP
Sun Oct 14 08:30:21 AEST 1984


------------------------------------------------------------------------------

   I've just started to play with shell scripts and had to say I totally agree
with Larry Tepper's complaint about the Berkeley 'head' program being binary
rather than just a shell script.  I guess the 'software tools' concept hasn't
cought on completely...

   Anyway, I tried Larry's sh script and found it generally satisfactory, but
it doesn't quite work right when you execute

			cat big_file | head.sh

(where 'head.sh' is Larry's script, and 'big_file' contained ~ 30K characters).
I guess the reason is that 'sed' has to do buffering when you use the 'i'
command within it.  Anyway, I thought I'd try my own version.  For 0 or 1 file,
it exec's to 'sed' for the sake of speed rather than running 'sed' and exiting.


# head: print the first lines of a stream.

PATH=/bin

case $1 in
   -[0-9]*)	count=`echo $1 | sed 's/-//'` ; shift ;;
   *)		count=10
esac
case $# in
   0|1)		exec sed ${count}q $1 ;;
   *)		echo "==> $1 <==" ; sed ${count}q $1 ; shift
esac
for f in $*
do
   echo "
==> $f <=="
   sed ${count}q $f
done


   I think this version is a little faster on the whole, and it avoids the
"Broken pipe" message that I get when I cat a large file through the former
version.

------------------------------------------------------------------------------

Ray Lubinsky
University of Virginia, Dept. of Computer Science
uucp: decvax!mcnc!ncsu!uvacs!rwl
AT$T: (804) 924-7280
mail: ...get serious, who mails anything these days?



More information about the Comp.unix mailing list