Skip to content

Instantly share code, notes, and snippets.

@ryo1kato
Created September 16, 2011 10:31
Show Gist options
  • Save ryo1kato/1221794 to your computer and use it in GitHub Desktop.
Save ryo1kato/1221794 to your computer and use it in GitHub Desktop.
monitor fs changes with inotify-tools and invoke make automatically
#!/bin/bash
interval=2
file_ignore='\.(swp|orig|old[0-9]*|bak[0-9]*)$|^core$|.*~$'
dir_ignore='(\.git|\.hg)'
make='make'
#####################################################
progname='maked'
usage () {
cat << "EOF"
Usage: $progname [OPTSIONS] [--] [MAKE_ARGS...]
Monitor changes under current directory using inotify, and
runs 'make'
EXAMPLE
maked -- -n
Run make with -n(--dry-run) option when changes are detected.
maked --interval 10 vmlinux
Try to build 'vmlinux' file every 10 sec, whenever change is detected.
OPTIONS
-i, --interval=SECONDS Minimum interval to run make.
-e, --file-exclude=REGEX Files to be ignored, as regular expression.
Default: /$file_ignore/
-d, --dir-exclude=REGEX Directories to be ignored, as regular expression
Default: /$dir_ignore/
-m, --make=COMMAND_NAME Specify alternative make command
-h, --help Show this help.
-v, --verbose Print detail information.
-- Indicate end of option, in case MAKE_ARGS starts
with dash(-)
EOF
}
MSG () { echo "$progname: $*"; }
DIE () { MSG "$@" >&2; exit 1; }
MSG_V () {
if [ "$opt_verbose" = yes ]; then
MSG "$@"
fi
}
optparse () {
local opt="$1"
local arg="$2"
local name="$3"
case $1 in
--*=*) eval "$name=\"${opt#*=}\""; return 1;;
-*) eval "$name=\"$arg\""; return 0;;
*) DIE "Internal Error"
esac
}
while [ $# -ge 1 ]
do
case $1 in
-e|--file-exclude*) optparse "$1" "$2" file_ignore || shift;;
-d|--dir-exclude*) optparse "$1" "$2" dir_ignore || shift;;
-m|--make*) optparse "$1" "$2" make || shift;;
-i|--interval*) optparse "$1" "$2" interval || shift;;
-h|--help) usage; exit 0;;
-v|--verbose) opt_verbose=yes;;
-*) DIE "Unknown option $1" ;;
--) break;;
esac
shift
done
#####################################################
last_executed=0
MSG_V "Started"
MSG_V " interval: $interval sec"
MSG_V " make command: $make"
MSG_V " file_ignore: $file_ignore"
MSG_V " dir_ignore: $dir_ignore"
inotifywait -e create -e modify -e delete -e move -r -m . 2>/dev/null |
while read dir event file
do
MSG_V "Event '$event' for file '$file' in '$dir'"
if [[ $event =~ ISDIR ]]
then
MSG_V "Ignoring change of '$dir'"\
"(changes for directories are ignored)"
elif [[ $dir =~ $dir_ignore ]]
then
MSG_V "Ignoring change in '$dir': match to dir_ignore pattern"
elif [[ $file =~ $file_ignore ]]
then
MSG_V "Ignoring change of '$dir': match to file_ignore pattern"
else
((remain = (last_executed + interval) - SECONDS))
if (( remain > 0 ))
then
MSG_V "$remain seconds to wait."
sleep $remain
fi
MSG_V "Executing: $make $*"
"$make" "$@"
last_executed=$SECONDS
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment