Skip to content

Instantly share code, notes, and snippets.

@ironjan
Created July 9, 2015 09:47
Show Gist options
  • Save ironjan/f2bf7e6f7d1fe0de229b to your computer and use it in GitHub Desktop.
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
#!/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[*]}"
@ironjan
Copy link
Author

ironjan commented Jul 9, 2015

@ironjan
Copy link
Author

ironjan commented Jul 9, 2015

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