Skip to content

Instantly share code, notes, and snippets.

@IanVaughan
Created January 3, 2012 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanVaughan/1556291 to your computer and use it in GitHub Desktop.
Save IanVaughan/1556291 to your computer and use it in GitHub Desktop.
Bash script that monitors a directory of source files for changes, then does something (like build) if changed.
#!/bin/bash
# Bash script that monitors a directory of source files for changes, then does something (like build) if changed.
# It does this via creating a SHA of the ls tree of the folder in question, and if changed, triggers the action.
# The "action" is just the variable $command, normally make.
# Usage :
# ./monitor.sh
# Then save a file, and it gets built! Magic!
command="make"
params=""
if [ $1 ]; then
echo "--> Monitor: Passing parameters :" $*
params="$*"
fi
sha=0
previous_sha=0
update_sha()
{
sha=`ls -lR . | egrep '\.cpp|\.h|\.spt' | sha1sum`
}
build () {
${command} ${params}
echo
echo "--> Monitor: Monitoring filesystem... (Press enter to force a build/update)"
}
changed () {
echo "--> Monitor: Files changed, Building..."
build
previous_sha=$sha
}
compare () {
update_sha
if [[ $sha != $previous_sha ]] ; then changed; fi
}
run () {
while true; do
compare
read -s -t 1 && (
echo "--> Monitor: Forced Update..."
build
)
done
}
echo "--> Monitor: Init..."
echo "--> Monitor: Monitoring filesystem... (Press enter to force a build/update)"
run
@IanVaughan
Copy link
Author

Bash script that monitors a directory of source files for changes, then does something (like build) if changed.
It does this via creating a SHA of the ls tree of the folder in question, and if changed, triggers the action.
The "action" is just the variable $command, normally make.
Usage :
./monitor.sh Then save a file, and it gets built! Magic!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment