Skip to content

Instantly share code, notes, and snippets.

@theepicsnail
Created August 17, 2014 02:18
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 theepicsnail/e75a143b0cd017c82054 to your computer and use it in GitHub Desktop.
Save theepicsnail/e75a143b0cd017c82054 to your computer and use it in GitHub Desktop.
alias watch="bash ~/watch.sh"
# watch foo.js bar.js -- ./runTests.sh
# runs runTests.sh, then waits for foo.js or bar.js to be modiified
# then reruns runTests.sh (kills the old process if it's still alive)
#!/bin/bash
# find --
split="-1"
for (( i = 0; i < ${#@}; i++ )); do
if [ "${!i}" == "--" ]
then
split=$i
i=${#@}
fi
done
if [ "$split" == "-1" ]
then
echo "Missing --"
echo "Expected $0 files ... -- command..."
exit 1
fi
FILES=${@:1:$split-1}
shift $split
function quit () {
end
exit 0
}
trap quit SIGINT
PID=0
function end() {
if [ $PID -ne 0 ]
then
kill $PID 2>&1 >/dev/null
echo "Killed $PID"
fi
}
function begin() {
$* &
echo "Started $*"
PID=$!
}
begin $*
while true
do
inotifywait -q -e MODIFY ${FILES[@]}
end
begin $*
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment