Created
December 15, 2017 03:58
-
-
Save ingydotnet/afc633e98436c7c0e0dd859398105936 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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