Skip to content

Instantly share code, notes, and snippets.

@vain
Created July 13, 2012 16:23
Show Gist options
  • Save vain/3105778 to your computer and use it in GitHub Desktop.
Save vain/3105778 to your computer and use it in GitHub Desktop.
watch-and-notify
#!/bin/bash
# Requires inofity-tools.
# Get filename to watch.
if [[ -z "$1" ]]
then
cat << EOM
Usage: $(basename $0) <filename> [-c <context>] [args to 'notify-send' ...]
EOM
exit 1
fi
fname="$1"
shift
# Lines of context?
context=10
if [[ "$1" == "-c" ]]
then
context="$2"
shift
shift
fi
# Make sure it exists.
if [[ ! -f "$fname" ]]
then
touch "$fname" || exit 1
fi
# Enter the watch loop.
while inotifywait -q -q -e close_write "$fname"
do
notify-send "$@" "$(tail -n "$context" "$fname" | tac)"
done
notify-send "$@" "Error: Can no longer watch \`$fname'!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment