Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- if [[ $# < 2 ]]; then
- echo -e Usage: watch.bash \<directory\> \<command\> \[\<argument placement\>\] \[\<extra arguments\>\]
- echo -e ' \t ' \<argument placement\> can be either "first" or "second"
- echo -e ' \t\t ' first: the changed file goes before the \<extra arguments\>
- echo -e ' \t\t ' second: the changed file goes fater the \<extra arguments\>
- echo -e ' \t\t ' \<argument placement\> defaults to "first"
- exit
- fi
- shopt -s extglob
- targetdir=${1%%+(/)}
- cmd=$2
- mode=${3:-"second"}
- if [ "$mode" = "first" ] || [ "$mode" = "second" ]; then
- shift
- else
- mode="first"
- fi
- shift
- shift
- echo Monitoring $targetdir
- while true; do
- pushd $targetdir >/dev/null
- change=$(inotifywait -q -e close_write,moved_to,create .)
- change="$targetdir/${change#./ * }"
- echo FULL PATH $change
- echo RELPATH $change
- change=`python -c "import os,sys;print(os.path.relpath(sys.argv[1],os.getcwd()))" "$change"`
- popd >/dev/null
- if [ "$mode" = "second" ]; then
- $cmd $@ "$change"
- else
- $cmd "$change" $@
- fi
- done
Add Comment
Please, Sign In to add comment