Tyler_Elric

watch.bash

Apr 9th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.07 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. if [[ $# < 2 ]]; then
  4.   echo -e Usage: watch.bash \<directory\> \<command\> \[\<argument placement\>\] \[\<extra arguments\>\]
  5.   echo -e ' \t ' \<argument placement\> can be either "first" or "second"
  6.   echo -e ' \t\t '     first: the changed file goes before the \<extra arguments\>
  7.   echo -e ' \t\t '    second: the changed file goes fater the \<extra arguments\>
  8.   echo -e ' \t\t '    \<argument placement\> defaults to "first"
  9.   exit
  10. fi
  11.  
  12. shopt -s extglob
  13. targetdir=${1%%+(/)}
  14.  
  15. cmd=$2
  16.  
  17. mode=${3:-"second"}
  18. if [ "$mode" = "first" ] || [ "$mode" = "second" ]; then
  19.   shift
  20. else
  21. mode="first"
  22. fi
  23. shift
  24. shift
  25.  
  26. echo Monitoring $targetdir
  27. while true; do
  28.   pushd $targetdir >/dev/null
  29.   change=$(inotifywait -q -e close_write,moved_to,create .)
  30.   change="$targetdir/${change#./ * }"
  31.   echo FULL PATH $change
  32.   echo RELPATH $change
  33.   change=`python -c "import os,sys;print(os.path.relpath(sys.argv[1],os.getcwd()))" "$change"`
  34.   popd >/dev/null
  35.   if [ "$mode" = "second" ]; then
  36.     $cmd $@ "$change"
  37.   else
  38.     $cmd "$change" $@
  39.   fi
  40. done
Add Comment
Please, Sign In to add comment