whenfilechanges with glob support
This commit is contained in:
38
qcd_function
38
qcd_function
@@ -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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user