Skip to content

Instantly share code, notes, and snippets.

@wookietreiber
Last active December 30, 2018 11:44
Show Gist options
  • Save wookietreiber/6145723 to your computer and use it in GitHub Desktop.
Save wookietreiber/6145723 to your computer and use it in GitHub Desktop.
do something when a file is modified, e.g. convert markdown to pdf: $ inotify-action foo.md pandoc -o foo.pdf foo.md
#!/usr/bin/env bash
declare -a watches
while [[ -n $1 && $1 != "--" ]]
do
watches+=("$1")
shift
done
# check if we have something to watch
if [[ ${#watches[@]} -eq 0 ]]
then
echo "usage: $(basename "$0") FILE... -- command" >&2
exit 1
fi
# shift away the double dashes
shift
# check if we have a command
if [[ -z $1 ]]
then
echo "usage: $(basename "$0") FILE... -- command" >&2
exit 1
fi
# start command once
"$@"
# now wait for changes and then start command
while inotifywait -qr -e modify "${watches[@]}" &> /dev/null
do
"$@"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment