Created
May 16, 2017 23:01
-
-
Save gwpl/fd1420d73480329d4e8e4e332d04535c to your computer and use it in GitHub Desktop.
inotifywait to exec specified command on each file change -> e.g. run "make", "pdflatex", or any other compile/upload or other command of choice! (Thanks https://superuser.com/a/181543/81861 ! )
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 | |
# Example usecase: | |
# inotify_exec_on_file_change.sh /path/phd paper.tex pdflatex paper.tex | |
dirn="$1" | |
filen="$2" | |
shift 2 | |
echo "Watching directory $dirn for changes of file $filen . Watching directory insteaf of file to overcome behaviour of many text editors that replace file - Thanks to Giles and see his answer https://superuser.com/a/181543/81861 for more details. In case of matching even I execute:" $@ | |
#inotifywait -m -e close_write,moved_to,create "$dirn" | | |
#inotifywait -m -e close_write "$dirn" | | |
#while read -r directory events filename; do | |
inotifywait -m -e close_write --format '%f' "$dirn" | | |
while read -r filename; do | |
echo "[inotifywait] "$directory $events $filename | |
if [ "$filename" = "$filen" ]; then | |
echo "[executing] " "$@" | |
"$@" | |
else | |
echo "[not executing] Due to mismatch in filename. Expected \"$filen\", but received event about \"$filename\"" | |
fi | |
done | |
echo loop reading from inotifywait finished with status $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment