Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- help() { cat <</help
- Loop examining file until it stops changing (better than \`watch ls -l FILE')
- Usage: $0 [-TIME] FILE[...]
- TIME is the wait time (default: 10s) between checks (see \`sleep --help')
- Example: ls_looped -5m foobar
- ls_looped v 0.4, (c) 2005-9 Adam Katz <scriptsATkhopiscom>, license: GPL v2+
- /help
- }
- # This would probably take 20-50% less code if written with bash arrays,
- # but I wanted full POSIX-shell compatibility. -Adam
- def_time=10 # default time in seconds
- if [ $# = 0 ] || [ "$1" != "${1#-[-hHVv]}" -a ! -e "$1" ]; then
- help
- exit 0
- fi
- [ "$1" != "${1#-}" ] && [ -n "$2" ] && time="${1##[-a-z]}" && shift
- i=0
- while [ $i -lt $# ]; do
- i=$((i+1))
- pending="${pending:-:}$i:"
- unset old old1$i old2$i
- done
- while [ -n "$1" -a -n "${pending#:}" ]; do
- get_listing() { ls -lpd ${LS_COLORS:+-h} ${LS_COLORS:+--color=always} "$@";}
- if [ $# = 1 ]
- then
- listing=`get_listing "$@"`
- printf "\r$(echo $listing)"
- else
- clear 2>/dev/null
- if [ "${time:-1}" -gt 0 ] 2>/dev/null
- then tmp="${time:-$def_time}s"
- else tmp="$time"
- fi
- tmp="$tmp looped listing of: $*"
- tmp2=`date +"%a %b %e %T %Y"`
- width="$((${#tmp}+${#tmp2}+4-${COLUMNS:-80}))" 2>/dev/null
- if ! [ "$width" -le 0 ] 2>/dev/null; then # we use ! ge to catch errors
- tmp3=`echo "$tmp" |sed -r "s/.{$width}$//" 2>/dev/null`
- if [ -z "$tmp3" ]; then
- tmp3="$tmp"
- i="$width"
- while [ "$i" -gt 0 ]; do
- tmp3="${tmp3%?}"
- i="$((i-1))"
- done
- fi
- tmp="$tmp3..."
- fi
- echo "$tmp $tmp2"
- get_listing "$@"
- fi
- # sleep for specified time; if it failed, reset time to 10s and use that
- if ! sleep $time 2>/dev/null; then time="$def_time"; sleep $time; fi
- i=1
- for file in "$@"; do
- listing=`ls -ln --full-time "$file" 2>/dev/null || ls -l "$file"`
- #eval old='$'old2$i # dash breaks POSIX std: http://tinyurl.com/n5r62o
- old="old2$i"
- old=`eval echo '$'$old`
- tmp1="${pending%:$i:*}" # i=3 pending=:1:2:3:4: -> tmp1=:1:2
- if [ "$listing" = "$old" ] # unchanged after three of the same
- then
- tmp2="${pending#*:$i:}" # i=3 pending=:1:2:3:4: -> tmp2=4:
- if [ -z "$tmp1$tmp2" ] # we have no pending files
- then unset pending # done
- elif [ "$tmp1" != "$tmp2" ] # if tmp1 and tmp2 are different
- then pending="$tmp1:$tmp2" # concatenate; ... -> pending=:1:2:4:
- fi # (otherwise, there was no hit and we should do nothing)
- else # changed
- if [ "$pending" = "$tmp1" ]; then # if changed but marked as unchanging
- pending="${pending:-:}$i:" # add i back to pending list
- fi
- fi
- #eval old2$i='$'old$i # dash breaks POSIX standard as noted above
- old="old1$i"
- old=`eval echo '$'$old`
- export old2$i="$old"
- export old1$i="$listing"
- i=$((i+1))
- done
- done
- if [ $# = 1 ]; then
- echo "" # the \n to end the last line
- fi
Add Comment
Please, Sign In to add comment