Skip to content

Instantly share code, notes, and snippets.

@nanawel
Created May 26, 2015 21:40
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 nanawel/1422a60c7e1e918c6681 to your computer and use it in GitHub Desktop.
Save nanawel/1422a60c7e1e918c6681 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Simple Directory Watcher
# Lists the content of the specified folder and uses zenity to display
# regularly its content as notifications on the desktop.
#
# Author: Nanawel 2015-04 - nanawel {at} remove-that gmail {dot} com
DIALOG_TIMEOUT=10 # seconds
POLLING_DELAY=30 # minutes
function echoerr {
echo "$@" 1>&2
}
function _check_req {
type -t zenity
if [ $? -ne 0 ]; then
echoerr "Missing zenity."
exit 1
fi
exit 0
}
function _print_usage {
echo -e "Usage: $0 directory [display = :0]"
}
function _display_notification {
zenity --notification --text="$1" --display="$2" --timeout=$DIALOG_TIMEOUT
}
function _run {
if [ -z "$1" ]; then
echoerr "Missing directory"
_print_usage
exit 1
fi
if [ ! -d "$1" ] || [ ! -r "$1" ]; then
echoerr "$1 is not a valid directory"
_print_usage
exit 1
fi
if [ ! -z "$2" ]; then
display="$2"
else
display=":0"
fi
DIR=$(readlink -f "$1")
while true; do
dir_content=$(ls -1t "$DIR")
if [ ! -z "$dir_content" ]; then
notification_content="$(echo -e "Files in $DIR:\n\n$dir_content")"
_display_notification "$notification_content" "$display"
fi
wait_time=$(expr 60 \* $POLLING_DELAY)
echo "Sleeping for $wait_time seconds..."
sleep $wait_time
done
}
if [ ! $(_check_req) ]; then
exit 1
fi
_run $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment