Listing 6: mxfl -- Improved standalone program to prune files
:
######################################################
# mxfl - maximum file length
######################################################
test $# = 2 || {
echo "usage: `basename $0` [filename] [maxlines]"
xstatus=1
exit 1
}
xstatus=0
# always cleanup
trap 'rm -f $tmpfile; exit $xstatus' 0 1 2 3 15
# if anything unusual happens, stop
set -e
test -f $1 || {
echo "Error: could not find: $1"
xstatus=2
exit 2
}
tmpfile=/tmp/mxfl.$$
# use tail to grab the last few lines. if
# things are ok, then cat the grabbed lines
# into the source file. cat is used because
# it leaves the ownership and permissions unchanged.
tail -${2} $1 > $tmpfile && cat $tmpfile > $1
xstatus=$?
|