pastebin - collaborative debugging tool
rovema.kpaste.net RSS


watchdiff.ksh - watch evolution of a file via diff
Posted by Anonymous on Mon 3rd Jul 2023 15:17
raw | new post

  1. #!/usr/bin/ksh93
  2.  
  3. #
  4. # diff watch - interactively watch evolution of file
  5. #
  6. # Example usage:
  7. # $ ./watchdiff.ksh /proc/fs/nfsd/clients/2497/states
  8. #
  9. # Written by Roland Mainz <roland.mainz@nrubsig.org>
  10. #
  11.  
  12. function diffvars
  13. {
  14.         # use nameref here for efficiency
  15.         nameref o=$1
  16.         nameref c=$2
  17.        
  18.         diff -u \
  19.                 <( cat <<<"$o" ) \
  20.                 <( cat <<<"$c" ) || true
  21. }
  22.  
  23. function main
  24. {
  25.         set -o errexit
  26.         set -o nounset
  27.  
  28.         typeset basefile="$1"
  29.         typeset orig_basefilecontent="$( < "$basefile" )"
  30.         typeset curr_basefilecontent="$orig_basefilecontent"
  31.  
  32.         integer term_lines
  33.         float   ts # timestamp
  34.         typeset usrcmd
  35.         typeset headtailcmd='head'
  36.  
  37.         for (( ;; )) ; do
  38.                 term_lines="$(tput lines)"
  39.                 tput clear
  40.  
  41.                 printf $"# %s: Watching file $'%q' in %s mode:\n" \
  42.                         "watchdiff.ksh" \
  43.                         "${basefile}" \
  44.                         "${headtailcmd}"
  45.  
  46.                 curr_basefilecontent="$( < "$basefile" )"
  47.                 diffvars orig_basefilecontent curr_basefilecontent 2>&1 | \
  48.                         ${headtailcmd} -n $(( term_lines - 3 ))
  49.  
  50.                 for (( ts=SECONDS ; (SECONDS-ts) < 10. ; )) ; do
  51.                         while read -n1 -t1 usrcmd ; do
  52.                                 case "${usrcmd}" in
  53.                                         $'r') # reload original file
  54.                                                 orig_basefilecontent="$curr_basefilecontent"
  55.                                                 break 2
  56.                                                 ;;
  57.                                         $' ') # refresh
  58.                                                 break 2
  59.                                                 ;;
  60.                                         $'h') # use head
  61.                                                 headtailcmd='head'
  62.                                                 break 2
  63.                                                 ;;
  64.                                         $'t') # use tail
  65.                                                 headtailcmd='tail'
  66.                                                 break 2
  67.                                                 ;;
  68.                                         $'q' | $'\E') # quit
  69.                                                 break 3
  70.                                                 ;;
  71.                                         *)
  72.                                                 printf $"Unknown cmd %q\n" "$usrcmd"
  73.                                                 ;;
  74.                                 esac
  75.                         done
  76.                 done
  77.         done
  78.        
  79.         return 0
  80. }
  81.  
  82. # main
  83. export PATH='/bin:/usr/bin'
  84.  
  85. builtin cat
  86. builtin head
  87.  
  88. main "$@"
  89. exit $?

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at