#! /bin/ksh # Get directory as parameter, list all files in this directory, # print them in reverse order and let the user select one. # then that file will be presented with either tail -f or less/more. # Enter will bring the user back to the dialog where he/she can also # quit. # # Copyright 2002 # Chr. Clemens Lee, http://www.kclee.com/clemens/ # 2002-01-17, last modified: $Date: 2006/11/18 17:17:09 $, $Revision: 1.2 $ # Distributed under the Gnu Public License (GPL). logdir=$1 test -z "$logdir" && echo "logwatcher: error: no log directory specified" && exit 2 process=$$ tempfile=/tmp/_log_list$process viewer=tail while true do echo Log Watcher # Print file list in reverse order with line numbers, # save file list in temp file. (ls -lt $logdir > $tempfile) && tail -$(expr $(wc -l $tempfile | cut -c0-8) - 1) $tempfile | nl | sort -nr # user command echo "q for quit, m for more, t for tail -f (default is $viewer)" read selection # skip empty input if test -z "$selection" then continue fi # # process quit or viewer selection # if test $selection = "q" then rm ${tempfile} exit 0 fi if test $selection = "t" then viewer=tail continue fi if test $selection = "m" then viewer=more continue fi # skip garbage if [[ "$selection" < "1" ]] then continue fi if [[ "$selection" > "999999999" ]] then continue fi maxline=$(expr $(wc -l $tempfile | cut -c0-8) - 1) if (( $selection < 1 )) then continue fi if (( $selection > $maxline )) then continue fi # select file line=$(tail -$(expr $(wc -l $tempfile | cut -c0-8) - $selection) $tempfile | head -1) file=${line##* } test -f $logdir/$file || continue # # view file # if test $viewer = "tail" then echo echo " to quit tail" echo tail -f $logdir/$file & TAIL_PID=$! read stop_tail # kill tail before printing file list again kill $TAIL_PID fi if test $viewer = "more" then # use less if it exists, otherwise use more whence less > /dev/null && less $logdir/$file whence less > /dev/null || more $logdir/$file # to have a clean start again echo fi done