From: Don Armstrong Date: Wed, 17 May 2017 21:43:48 +0000 (-0700) Subject: support no inotifywait X-Git-Url: https://git.donarmstrong.com/?p=bin.git;a=commitdiff_plain;h=d7bb4dc4044f7fb34f498fa3cd4a39e754b7d202;hp=2486fcb5ada0ff7f32f257f901808598ec865490 support no inotifywait --- diff --git a/run_when_changed b/run_when_changed index 85c4c34..8ad3a27 100755 --- a/run_when_changed +++ b/run_when_changed @@ -1,14 +1,29 @@ #!/bin/sh -WATCH_FILE="$1" +WATCH_FULL_FILE="$1" shift 1; -WATCH_DIR=`dirname "$WATCH_FILE"` -WATCH_FILE=`basename "$WATCH_FILE"` +if ! [ -e "$WATCH_FULL_FILE" ]; then + echo "No such file or directory $WATCH_FULL_FILE"; +fi; -inotifywait -m -e close_write,moved_to,create $WATCH_DIR | - while read -r directory events filename; do - if [ "x$filename" = "x$WATCH_FILE" ]; then +WATCH_DIR=`dirname "$WATCH_FULL_FILE"` +WATCH_FILE=`basename "$WATCH_FULL_FILE"` + +if which inotifywait > /dev/null 2>&1; then + inotifywait -m -e close_write,moved_to,create $WATCH_DIR | + while read -r directory events filename; do + if [ "x$filename" = "x$WATCH_FILE" ]; then + ( exec "$@" ); + fi; + done; +else + CURR_TIME=$(stat -c '%Y' "$WATCH_FULL_FILE"); + while sleep 1; do + NEW_TIME=$(stat -c '%Y' "$WATCH_FULL_FILE"); + if [ "x$NEW_TIME" != "x$CURR_TIME" ]; then + CURR_TIME=$NEW_TIME ( exec "$@" ); fi; done; +fi;