#!/usr/bin/ksh93 # # diff watch - interactively watch evolution of file # # Example usage: # $ ./watchdiff.ksh /proc/fs/nfsd/clients/2497/states # # Written by Roland Mainz # function diffvars { # use nameref here for efficiency nameref o=$1 nameref c=$2 diff -u \ <( cat <<<"$o" ) \ <( cat <<<"$c" ) || true } function main { set -o errexit set -o nounset typeset basefile="$1" typeset orig_basefilecontent="$( < "$basefile" )" typeset curr_basefilecontent="$orig_basefilecontent" integer term_lines float ts # timestamp typeset usrcmd typeset headtailcmd='head' for (( ;; )) ; do term_lines="$(tput lines)" tput clear printf $"# %s: Watching file $'%q' in %s mode:\n" \ "watchdiff.ksh" \ "${basefile}" \ "${headtailcmd}" curr_basefilecontent="$( < "$basefile" )" diffvars orig_basefilecontent curr_basefilecontent 2>&1 | \ ${headtailcmd} -n $(( term_lines - 3 )) for (( ts=SECONDS ; (SECONDS-ts) < 10. ; )) ; do while read -n1 -t1 usrcmd ; do case "${usrcmd}" in $'r') # reload original file orig_basefilecontent="$curr_basefilecontent" break 2 ;; $' ') # refresh break 2 ;; $'h') # use head headtailcmd='head' break 2 ;; $'t') # use tail headtailcmd='tail' break 2 ;; $'q' | $'\E') # quit break 3 ;; *) printf $"Unknown cmd %q\n" "$usrcmd" ;; esac done done done return 0 } # main export PATH='/bin:/usr/bin' builtin cat builtin head main "$@" exit $?