#!/bin/sh # # A small script for running a command whenever the specified files (or # any file in the current directory) changes. # # Requires the inotifywait command which is part of inotify-tools. # # By Ted Percival (2007) # Hereby dedicated to the public domain: # http://creativecommons.org/licenses/publicdomain/ # set -e [ $# -gt 0 ] || exit 1 CMD=$1 shift [ $# -eq 0 ] && set -- `pwd` # I'm not sure if these are all necessary, but the way vim writes files # requires that some kind of delete event be checked for. EVENTFLAGS="-e modify -e close_write -e delete -e delete_self -e create" run_cmd () { set +e $CMD ret=$? set -e echo if [ $ret -eq 0 ]; then echo Command succeeded. else echo Command returned $ret. fi # Make sure the editor has time to put the files back # I know this is bogus, but it seems to help. sleep 1 return 0 } # Run once to start with run_cmd while inotifywait -qq $EVENTFLAGS "$@"; do run_cmd done exit 0 # vim: ts=2 sw=2 et tw=72 syntax=sh ai