From a0bcaf779e11b6e5b0305e11ca8378532f17c2f6 Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Sun, 6 Oct 2013 14:15:44 +0200 Subject: [PATCH] initial commit --- README.md | 9 +++++++++ watch | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 README.md create mode 100755 watch diff --git a/README.md b/README.md new file mode 100644 index 0000000..c305c19 --- /dev/null +++ b/README.md @@ -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 diff --git a/watch b/watch new file mode 100755 index 0000000..6300957 --- /dev/null +++ b/watch @@ -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