whenfilechanges with glob support

This commit is contained in:
ville rantanen
2014-08-22 09:43:20 +03:00
parent 659de6edbf
commit 1d66b7da9a

View File

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