Created
July 9, 2015 09:47
-
-
Save ironjan/f2bf7e6f7d1fe0de229b to your computer and use it in GitHub Desktop.
Usage: watcher.sh <watched files> -- <command>; watcher then will execute the given command whenever a write has occured to one of the watched files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
INDEX=0 | |
IS_PARSING_WATCHED_FILES="y" | |
declare -a WATCHED_FILES | |
declare -a COMMAND | |
while [[ $# > 0 ]] | |
do | |
if [ "$1" == "--" ] | |
then | |
IS_PARSING_WATCHED_FILES="n" | |
INDEX=0 | |
else | |
if [ "$IS_PARSING_WATCHED_FILES" = "y" ] | |
then | |
WATCHED_FILES[$INDEX]=$1 | |
else | |
COMMAND[$INDEX]=$1 | |
fi | |
fi | |
INDEX=$((INDEX + 1)) | |
shift | |
done | |
if [ ${#COMMAND[@]} -lt 1 ] | |
then | |
echo "Usage: watcher.sh <watched files> -- <command>" | |
exit 1 | |
fi | |
while inotifywait -e close_write ${WATCHED_FILES[*]} | |
do | |
${COMMAND[*]} | |
done | |
echo "Watching: ${WATCHED_FILES[*]}" | |
echo "Executing: ${COMMAND[*]}" | |
BTW: requires inotify-tools on arch/manjaro
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is based on the answer of http://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes