Skip to content

Instantly share code, notes, and snippets.

@davidmoreno
Last active March 29, 2021 08:27
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save davidmoreno/c049e922e41aaa94e18955b9fac5549c to your computer and use it in GitHub Desktop.
Watches a diretory and runs a command on save changes.
#!/bin/bash
if [ ! "$1" ]; then
cat << EOF
$0 -- Watch a directory and run a command on changes
$0 cmd options
Environmental vars:
WATCHDIR -- Direcotry to watch, by default current
EOF
exit 0
fi
WATCHDIR=$1
shift 1
while true; do
echo -e "\e[6;37;46m \n \n$*\n \e[0m"
sh -c "$*"
exit_code=$?
if [ "$exit_code" = 0 ]; then
echo -e "\e[6;37;44m\nEXIT $?\e[0m"
else
echo -e "\e[6;37;41m\nEXIT $?\e[0m"
fi
echo
sleep 1
inotifywait -re close_write $WATCHDIR
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment