Published: Thu 11 October 2012
By michaelp
In misc .
tags: scripts utilities
A few months ago, when I started using Scala more seriously, I
familiarised myself with sbt after seeing it touted as the recommended
build tool here and there. Despite some grievances with this
not-so-simple build tool, I did find one of its features rather handy:
automatically executing compile or test actions whenever a file changed
(quite similar to hakyll’s preview feature).
In fact, I got used to this feature so much that I decided I needed it
for other projects, like automatically running LaTeX for me, or updating
ikiwiki .
It turns out this was fairly simple to implement with inotify-tools
doing all the heavy lifting, with regard to monitoring files for changes
and generating appropriate events. All that remained was wrapping it up
in a suitable script I called monitor , to only act on files that
match a particular regular expression:
TARGET="$1"
PATTERN="$2"
COMMAND=("${@:3}")
[[ $# -lt 3 ]] && {
echo "usage: $0 <watched-dir> <file-pattern> <command>"
exit 1 }
EVENTS=(modify delete)
inotifywait -r -m -e ${(j.,.)EVENTS} --format "%w%f" $TARGET \
| egrep --line-buffered "$PATTERN" | while read file; do
echo "event: $file"
$COMMAND
done
I could then run the following command to continuously build my ikiwiki:
monitor path/to/wiki '\.mkdn$|\.mdwn$' ikiwiki --setup ~/wiki.setup
or generate my LaTeX reports (given a suitable Makefile):
monitor path/to/tex '\.tex$|\.bib$' make
The script is also available on github as part of a collection of
scripts I have found a use for from time to time.