shosei

ls-looped

May 11th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.03 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. help() { cat <</help
  4. Loop examining file until it stops changing (better than \`watch ls -l FILE')
  5. Usage:  $0 [-TIME] FILE[...]
  6.  TIME is the wait time (default: 10s) between checks (see \`sleep --help')
  7. Example:  ls_looped -5m foobar
  8. ls_looped v 0.4, (c) 2005-9 Adam Katz <scriptsATkhopiscom>, license: GPL v2+
  9. /help
  10. }
  11. # This would probably take 20-50% less code if written with bash arrays,
  12. # but I wanted full POSIX-shell compatibility.  -Adam
  13.  
  14. def_time=10 # default time in seconds
  15.  
  16. if [ $# = 0 ] || [ "$1" != "${1#-[-hHVv]}" -a ! -e "$1" ]; then
  17.   help
  18.   exit 0
  19. fi
  20.  
  21. [ "$1" != "${1#-}" ] && [ -n "$2" ] && time="${1##[-a-z]}" && shift
  22.  
  23. i=0
  24. while [ $i -lt $# ]; do
  25.   i=$((i+1))
  26.   pending="${pending:-:}$i:"
  27.   unset old old1$i old2$i
  28. done
  29. while [ -n "$1" -a -n "${pending#:}" ]; do
  30.  
  31.   get_listing() { ls -lpd ${LS_COLORS:+-h} ${LS_COLORS:+--color=always} "$@";}
  32.   if [ $# = 1 ]
  33.     then
  34.       listing=`get_listing "$@"`
  35.       printf "\r$(echo $listing)"
  36.     else
  37.       clear 2>/dev/null
  38.       if [ "${time:-1}" -gt 0 ] 2>/dev/null
  39.         then tmp="${time:-$def_time}s"
  40.         else tmp="$time"
  41.       fi
  42.       tmp="$tmp looped listing of: $*"
  43.       tmp2=`date +"%a %b %e %T %Y"`
  44.       width="$((${#tmp}+${#tmp2}+4-${COLUMNS:-80}))" 2>/dev/null
  45.       if ! [ "$width" -le 0 ] 2>/dev/null; then # we use ! ge to catch errors
  46.         tmp3=`echo "$tmp" |sed -r "s/.{$width}$//" 2>/dev/null`
  47.         if [ -z "$tmp3" ]; then
  48.           tmp3="$tmp"
  49.           i="$width"
  50.           while [ "$i" -gt 0 ]; do
  51.             tmp3="${tmp3%?}"
  52.             i="$((i-1))"
  53.           done
  54.         fi
  55.         tmp="$tmp3..."
  56.       fi
  57.       echo "$tmp $tmp2"
  58.       get_listing "$@"
  59.   fi
  60.  
  61.   # sleep for specified time; if it failed, reset time to 10s and use that
  62.   if ! sleep $time 2>/dev/null; then time="$def_time"; sleep $time; fi
  63.  
  64.   i=1
  65.   for file in "$@"; do
  66.  
  67.     listing=`ls -ln --full-time "$file" 2>/dev/null || ls -l "$file"`
  68.     #eval old='$'old2$i    # dash breaks POSIX std: http://tinyurl.com/n5r62o
  69.     old="old2$i"
  70.     old=`eval echo '$'$old`
  71.  
  72.     tmp1="${pending%:$i:*}"     # i=3 pending=:1:2:3:4: -> tmp1=:1:2
  73.     if [ "$listing" = "$old" ]  # unchanged after three of the same
  74.     then
  75.       tmp2="${pending#*:$i:}"   # i=3 pending=:1:2:3:4: -> tmp2=4:
  76.       if [ -z "$tmp1$tmp2" ]    # we have no pending files
  77.         then unset pending      # done
  78.       elif [ "$tmp1" != "$tmp2" ]   # if tmp1 and tmp2 are different
  79.         then pending="$tmp1:$tmp2"      # concatenate;  ... -> pending=:1:2:4:
  80.       fi  # (otherwise, there was no hit and we should do nothing)
  81.     else                # changed
  82.       if [ "$pending" = "$tmp1" ]; then # if changed but marked as unchanging
  83.         pending="${pending:-:}$i:"    # add i back to pending list
  84.       fi
  85.     fi
  86.  
  87.     #eval old2$i='$'old$i   # dash breaks POSIX standard as noted above
  88.     old="old1$i"
  89.     old=`eval echo '$'$old`
  90.     export old2$i="$old"
  91.     export old1$i="$listing"
  92.  
  93.     i=$((i+1))
  94.   done
  95.  
  96. done
  97.  
  98. if [ $# = 1 ]; then
  99.   echo "" # the \n to end the last line
  100. fi
Add Comment
Please, Sign In to add comment