initial commit
This commit is contained in:
commit
a0bcaf779e
9
README.md
Normal file
9
README.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# Watcher
|
||||||
|
|
||||||
|
This is a simple script with watch for file modification and execute a
|
||||||
|
command when this occurs.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
watch ./gen fic1 fic2 dir1
|
||||||
|
watch "echo 'This file changed:' " dir1 dir2
|
55
watch
Executable file
55
watch
Executable file
|
@ -0,0 +1,55 @@
|
||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
error(){print -- $* >&2; exit 1}
|
||||||
|
|
||||||
|
(($#<2)) && error "usage: ${0:t} [once] cmd dirs..."
|
||||||
|
|
||||||
|
ONCE=0
|
||||||
|
[[ $1 = "once" ]] && { ONCE=1; shift }
|
||||||
|
|
||||||
|
(($#<2)) && error "usage: ${0:t} [once] cmd dirs..."
|
||||||
|
|
||||||
|
cmd="$1"
|
||||||
|
shift
|
||||||
|
|
||||||
|
typeset -a listDir
|
||||||
|
listDir=( $@ )
|
||||||
|
|
||||||
|
isRecentlyModified() {
|
||||||
|
local fic="$1"
|
||||||
|
local mtime=0
|
||||||
|
currenttime=$(date +"%s")
|
||||||
|
case $(uname) in
|
||||||
|
Darwin) mtime=$(stat -f %m $fic);;
|
||||||
|
Linux) mtime=$(stat --printf %Y $fic);;
|
||||||
|
*) mtime=$(stat --printf %Y $fic);;
|
||||||
|
esac
|
||||||
|
(( $currenttime - $mtime < 2 ))
|
||||||
|
}
|
||||||
|
|
||||||
|
checkfile=".last_watched"
|
||||||
|
((ONCE == 0)) && \
|
||||||
|
[[ -e $checkfile ]] && \
|
||||||
|
isRecentlyModified $checkfile && \
|
||||||
|
error "Wait at least 2 second if you killed the process"
|
||||||
|
|
||||||
|
execIfChanged() {
|
||||||
|
isRecentlyModified $1 && { print -- "$cmd $1"; eval "$cmd $1"}
|
||||||
|
}
|
||||||
|
|
||||||
|
typeset -A listFic
|
||||||
|
while true; do
|
||||||
|
listFic=()
|
||||||
|
for d in $listDir; do
|
||||||
|
if [[ -d $d ]]; then
|
||||||
|
for fic in $d/**/*(.); do
|
||||||
|
execIfChanged $fic
|
||||||
|
done
|
||||||
|
else
|
||||||
|
execIfChanged $d
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
((ONCE == 1)) && break
|
||||||
|
touch $checkfile
|
||||||
|
sleep 2
|
||||||
|
done
|
Loading…
Reference in a new issue