Created
May 26, 2015 21:40
-
-
Save nanawel/1422a60c7e1e918c6681 to your computer and use it in GitHub Desktop.
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 | |
# 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