Skip to content

Instantly share code, notes, and snippets.

@mkormendy
Forked from mikesmullin/watch.sh
Last active October 23, 2015 19:35
Show Gist options
  • Save mkormendy/5e7e1e79cb9df1c8c1d1 to your computer and use it in GitHub Desktop.
Save mkormendy/5e7e1e79cb9df1c8c1d1 to your computer and use it in GitHub Desktop.
react is a linux bash script to monitor file modification recursively and execute bash commands as changes occur
#!/usr/bin/env bash
# script: react (renamed from conflicting 'watch' command)
# author: Mike Smullin <mike@smullindesign.com>
# contributor: Mike Kormendy <mike@somethinginteractive.com>
# license: GPLv3
# description:
# watches the given path for changes
# and executes a given command when changes occur
# usage:
# watch <path> <cmd...>
#
path=$1
shift
cmd=$*
sha=0
update_sha() {
sha=`ls -lR --time-style=full-iso $path | sha1sum`
}
update_sha
previous_sha=$sha
react() {
echo -en " reacting...\n\n"
$cmd
echo -en "\n--> resumed watching."
}
compare() {
update_sha
if [[ $sha != $previous_sha ]] ; then
echo -n "change detected,"
react
previous_sha=$sha
fi
}
trap react SIGINT
trap exit SIGQUIT
echo -e "--> Press Ctrl+C to force reaction, Ctrl+\\ to exit"
echo -en "--> watching \"$path\""
while true; do
compare
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment