Created
May 3, 2012 18:39
-
-
Save poulter7/2587999 to your computer and use it in GitHub Desktop.
sleep
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 | |
SCRIPTNAME=`basename "$0"` | |
print_help() { | |
cat << EOF | |
Usage: $SCRIPTNAME filename | |
Uses 'inotifywait' to sleep until 'filename' has been modified. | |
Inspired by http://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes/181543#181543 | |
TODO: rewrite this as a simple Python script, using pyinotify | |
EOF | |
} | |
# parse_parameters: | |
while [[ "$1" == -* ]] ; do | |
case "$1" in | |
-h|-help|--help) | |
print_help | |
exit | |
;; | |
--) | |
#echo "-- found" | |
shift | |
break | |
;; | |
*) | |
echo "Invalid parameter: '$1'" | |
exit 1 | |
;; | |
esac | |
done | |
if [ "$#" != 1 ] ; then | |
echo "Incorrect parameters. Use --help for usage instructions." | |
exit 1 | |
fi | |
FULLNAME="$1" | |
BASENAME=`basename "$FULLNAME"` | |
DIRNAME=`dirname "$FULLNAME"` | |
coproc INOTIFY { | |
inotifywait -q -m -e close_write,moved_to,create ${DIRNAME} & | |
trap "kill $!" 1 2 3 6 15 | |
wait | |
} | |
trap "kill $INOTIFY_PID" 0 1 2 3 6 15 | |
# BUG! Não vai funcionar com arquivos contendo caracteres estranhos | |
sed --regexp-extended -n "/ (CLOSE_WRITE|MOVED_TO|CREATE)(,CLOSE)? ${BASENAME}\$/q" 0<&${INOTIFY[0]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment