#!/bin/bash # # A utility to execute a command and notify the user upon completion. # # Copyright 2009, Victor Chudnovsky # Credits: This work was inspired by http://wagiaalla.com/blog/?p=28 and # http://emacs-fu.blogspot.com/2009/11/showing-pop-ups.html # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. This program is distributed in # the hope that it will be useful, but WITHOUT ANY WARRANTY; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. See the GNU General Public License for more # details. You should have received a copy of the GNU General Public # License along with this program. If not, see # . basename=$(basename $0) dbg="$basename: " # form_option flag value # # Returns the string "--flag value" unless value is empty, in which # case it returns the empty string function form_option() { local flag=$1 local value=$2 local option="" if [[ -n "$value" ]] then option="$flag $value" fi echo $option } # set_one_flag_value command_root flag value_root variant # # Sets the global script variable ${command_root}_${variant} to be the # string needed to set a flag called $flag to the global script variable value # ${value_root}_${variant}. function set_one_flag_value() { local command_root=$1 local flag=$2 local value_root=$3 local variant=$4 local cmd="${command_root}_${variant}=\$(form_option \"$flag\" \$${value_root}_${variant})" eval $cmd } # set_success_failure_flag_variants command_root flag value_root # # Sets the global script variable ${command_root}_success and # ${command_root}_failure to be the strings needed to set a flag # called $flag to the global script variables ${value_root}_success # and ${value_root}_failure, respectively. function set_success_failure_flag_variants() { local command_root=$1 local flag=$2 local value_root=$3 set_one_flag_value $command_root $flag $value_root success set_one_flag_value $command_root $flag $value_root failure } # show_flag_variants command_root # # For debugging purposes, displays the global script variables # ${command_root}_success and ${command_root}_failure. function show_flag_variants() { local command_root=$1 eval "echo $dbg \"\" ${command_root}_success : \${${command_root}_success}" eval "echo $dbg \"\" ${command_root}_failure : \${${command_root}_failure}" } # get_argument argument # # If argument is of the form --XXX, returns XXX. Otherwise, returns # the empty string. function get_argument() { local argument="$1" expr "$argument" : "--\([a-z_]\+\)" } # debug arg1 arg2... # # If debug mode is on, echo arg1, arg2... to stderr. function debug() { if [[ -n "$debug" ]] then while [[ -n "$1" ]] do echo "$1" 1>&2 shift done fi } # show_usage # # Echoes usage information for this utility function show_usage() { cat <`date +%c`") # Build the appropriate notification command. cmd="notify-send \$show_icon_$result \$expiration_$result \$urgency_$result \"\$label_$result\"" debug "$dbg $cmd \"$content\"" debug "$dbg $(eval echo $cmd \"$content\")" # Notify, redirecting any stdout to stderr so as to allow this utility # to be used in stdout pipes. eval "$cmd \"$content\"" 1>&2 # Pass along the exit code of the command to the calling process. exit $exit_code