Skip to content

Instantly share code, notes, and snippets.

@poulter7
Created May 3, 2012 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poulter7/2587999 to your computer and use it in GitHub Desktop.
Save poulter7/2587999 to your computer and use it in GitHub Desktop.
sleep
#!/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