diff --git a/qcd_function b/qcd_function index 0b50f6d..f7f6035 100644 --- a/qcd_function +++ b/qcd_function @@ -140,28 +140,40 @@ Keeps a history of folders visited in ~/.bash_cdhistory function whenfilechanges() { - [ -z "$1" ] && { echo 'Usage: whenfilechanges "file" "some" "command" + [ -z "$2" ] && { echo 'Usage: whenfilechanges "file" "some" "command" Follows the modification time of the "file" and runs the command - at every change. ' - return - } - [ -e "$1" ] || { echo $1 not found + at every change. + The "file" may also be a glob e.g. "*tex" ' return } + local fname local otime local ntime + local i - fname=$1 + fname=($1) + echo Waiting for ${#fname[@]} files to change: ${fname[@]} shift 1 - otime=$( stat -c %Z "$fname" ) - while : - do ntime=$( stat -c %Z "$fname" ) - [ "$ntime" -ne "$otime" ] && { - eval "$@" - otime=$( stat -c %Z "$fname" ) + echo "Command: \"$@\"" + otime=() + for ((i=0;i<${#fname[@]};i++)) + do [ -e "${fname[$i]}" ] || { + echo "File: ${fname[$i]} not found!" + return } - + otime[$i]=$( stat -c %Z "${fname[$i]}" ) + done + while : + do ntime=() + for ((i=0;i<${#fname[@]};i++)) + do ntime[$i]=$( stat -c %Z "${fname[$i]}" ) + [ "${ntime[$i]}" -ne "${otime[$i]}" ] && { + echo "${fname[$i]} changed:" + eval "$@" + otime[$i]=$( stat -c %Z "${fname[$i]}" ) + } + done sleep 2 done