Skip to content

Instantly share code, notes, and snippets.

@ingydotnet
Created December 15, 2017 03:58
Show Gist options
  • Save ingydotnet/afc633e98436c7c0e0dd859398105936 to your computer and use it in GitHub Desktop.
Save ingydotnet/afc633e98436c7c0e0dd859398105936 to your computer and use it in GitHub Desktop.
#!/bin/bash
die() { echo "$*"; exit 1; }
main() {
program=$0
[[ $# -ge 2 ]] ||
die "usage: $program <watch-file> <command> [<command-args>]"
file=${1:?first arg must be file path}; shift
type "$1" &>/dev/null ||
die "second arg must be valid command"
export WATCH_AND_RUN_FILE=$file
mark=$(mktemp)
run-loop "$@" &
echo "PID = $!"
}
run-loop() {
while true; do
if [[ $file -nt $mark ]]; then
touch "$mark"
echo
echo "Changed: $file"
echo "Running: $*"
"$@"
fi
sleep 0.1
done
}
[[ $0 != "$BASH_SOURCE" ]] || main "$@"
# vim: set ft=sh:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment