run-on-change.sh 570 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. SCRIPT_NAME="${0##*/}"
  4. display_help() {
  5. echo "Usage: ${SCRIPT_NAME} <other commands>"
  6. echo "Dependencies: inotify-tools"
  7. echo "Examples:"
  8. echo "- ${SCRIPT_NAME} rsync -ruhP ./dir sever:/path/idk"
  9. exit 2
  10. }
  11. message(){
  12. info="$*"
  13. echo "==== $(date +'%F_%H%M%S') ==== ${info} ===="
  14. }
  15. for dir in "$@"; do
  16. [[ -d $dir ]] && DIR="$dir"
  17. done
  18. [[ $# -lt 2 && ! -d ${DIR:-} ]] && display_help
  19. while inotifywait -r -e modify,create,move "$DIR"; do
  20. message "Running: ${*}"
  21. "$@"
  22. done