Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash -
- # vim: set tabstop=8 softtabstop=4 shiftwidth=4 smarttab noexpandtab:
- ${DEBUG}
- #========================================================================
- # FILE: filename
- #
- # USAGE: filename [-h|--help][-V|--version]|[-l|--list]
- # -l
- # --list list alarms
- # -h
- # --help display program help and exit
- #
- # -u
- # --usage display a usage message and exit
- #
- # -V
- # --version display the program version number and exit
- # -x display debugging information
- #
- # DESCRIPTION: This template is a starting point for creating full-
- # featured shell scripts.
- #
- # OPTIONS: --help, --version
- # REQUIREMENTS: ---
- # BUGS: ---
- # NOTES: ---
- # AUTHOR: Scott Bicknell (SB), [email protected]
- # COMPANY:
- # VERSION: 1.0
- # CREATED:
- # REVISION: ---
- #
- # Copyright 2010 Scott Bicknell, sbicknel (at) gmail dot com
- #
- # 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 <http://www.gnu.org/licenses/>.
- #========================================================================
- PROGNAME="${0##*/}"
- VERSION='1.0'
- PATH="${HOME}/bin:/sbin:/bin:/usr/bin:/usr/local/bin"
- # honor the user's editor preference
- if [[ -n $EDITOR ]]; then
- editor=$EDITOR
- elif [[ -n $VISUAL ]]; then
- editor=$VISUAL
- else
- editor=/usr/bin/vi
- fi
- umask 077
- # -----------------------------------------------------------------------
- # Display error messages on standard error.
- # -----------------------------------------------------------------------
- error () {
- printf '%s: %s\n' $PROGNAME "$@"
- } >&2
- # -----------------------------------------------------------------------
- # Display messages on standard output.
- # -----------------------------------------------------------------------
- message () {
- printf '%s: %s\n' $PROGNAME "$@"
- }
- # -----------------------------------------------------------------------
- # Remove temporary files. Add additional file names, if necessary.
- # -----------------------------------------------------------------------
- clean_up () {
- rm -f ${TEMP_DIR}/${PROGNAME}.$$.*
- }
- # -----------------------------------------------------------------------
- # toggle write permission on or off. If turning them on, only give them
- # to the owner. If turning them off, turn them off for everyone. This
- # function operates on a list of files or folders.
- # -----------------------------------------------------------------------
- toggleWritePermissions () {
- for f in $@; do
- owp=$(stat --printf='%A' $f) # owner's write permissions
- owp=${owp:2:1}
- case $owp in
- -)
- chmod u+w $f
- ;;
- w)
- chmod a-w $f
- ;;
- esac
- done
- }
- # -----------------------------------------------------------------------
- # Function for exit due to fatal program error
- # Accepts 1 argument:
- # string containing descriptive error message
- # -----------------------------------------------------------------------
- error_exit () {
- error "${1:-'Unknown Error'}"
- clean_up
- exit 1
- }
- # -----------------------------------------------------------------------
- # Function to handle termination signals
- # Accepts 1 argument:
- # signal_spec
- # -----------------------------------------------------------------------
- signal_exit () {
- case $1 in
- INT)
- error_exit 'Program aborted by user'
- ;;
- TERM)
- error_exit 'Program terminated'
- ;;
- *)
- error_exit 'Terminating on unknown signal'
- ;;
- esac
- }
- trap 'signal_exit TERM' TERM HUP QUIT PIPE
- trap 'signal_exit INT' INT
- # -----------------------------------------------------------------------
- # Function called for a graceful exit
- # No arguments
- # -----------------------------------------------------------------------
- graceful_exit () {
- clean_up
- exit
- }
- # -----------------------------------------------------------------------
- # Function to create temporary files
- # optional number of files to create--default is one
- # returns an array ($TEMP_FILE) containing temporary file names
- # -----------------------------------------------------------------------
- make_temp_files () {
- declare fnum=${1:-1} # number of temporary files to create
- # Use user's local tmp directory if it exists
- if [[ -d ~/tmp ]]; then
- TEMP_DIR=~/tmp
- else
- TEMP_DIR=/tmp
- fi
- # Temp files for this script, using paranoid method of creation to
- # insure that file names are not predictable. This is for security to
- # avoid "tmp race" attacks.
- for (( n=0; n<$fnum; n++ )); do
- TEMP_FILE[$n]=$(mktemp -q "${TEMP_DIR}/${PROGNAME}.$$.XXXXXX")
- if [[ "${TEMP_FILE[$n]}" = '' ]]; then
- error_exit 'cannot create temp file!'
- fi
- done
- }
- # -----------------------------------------------------------------------
- # Display a short message about program usage. This is also used by the
- # helptext function to display part of its help text.
- # -----------------------------------------------------------------------
- usage () {
- cat <<- EOF
- USAGE: $PROGNAME: [-h|--help][-u|--usage][-V|--version][-x]|[-l|--list]
- EOF
- } >&2
- # -----------------------------------------------------------------------
- # Display the program version number
- # -----------------------------------------------------------------------
- version () {
- printf '%s %s\n' $PROGNAME $VERSION
- }
- # -----------------------------------------------------------------------
- # Function to display help message for program
- # No arguments
- # -----------------------------------------------------------------------
- helptext () {
- local tab=$(printf '\t\t')
- usage
- cat <<- -EOF-
- DESCRIPTION:
- Create one-time reminders. This program requires either kdialog or
- zenity and a valid X \$DISPLAY setting.
- Options:
- -l, --list List alarms
- -h, --help Display this help message and exit.
- -u, --usage Display a usage message and exit.
- -V, --version Display program version and exit.
- -x Display debugging output when running the program
- -EOF-
- version
- } >&2
- # -----------------------------------------------------------------------
- # list pending alarms
- # -----------------------------------------------------------------------
- listAlarms () {
- # get a list of at jobs
- jobs=( $(atq | awk '{print $1}') )
- # get a list of at jobs containing the text "Alarm"
- for job in ${jobs[@]}; do
- if at -c $job | grep -q 'Alarm'; then
- alarms=( ${alarms[@]} $job )
- fi
- done
- # check for an empty list. Create error message if needed
- if [[ ${#alarms[@]} -eq 0 ]]; then
- alarms[0]='No alarms to list'
- kdialog --msgbox "${alarms[0]}" --title "${PROGNAME^}" 2>/dev/null
- graceful_exit
- fi
- # display the resulting list
- alarmList=$"$(
- for alarm in ${alarms[@]}; do
- atq | grep "^$alarm"
- done | sort
- )"
- case $dialog in
- kdialog)
- kdialog --msgbox "$alarmList" --title "${PROGNAME^}" 2>/dev/null
- ;;
- zenity)
- zenity --info --text="$alarmList" --title="${PROGNAME^}" 2>/dev/null
- ;;
- esac
- }
- # -----------------------------------------------------------------------
- # Verify that the usage function and the helptext function both have
- # documentation for each long option. Verify that the header comment
- # also has each long option listed.
- # -----------------------------------------------------------------------
- if [[ "$1" = '--help' ]]; then
- helptext
- graceful_exit
- fi
- if [[ "$1" = '--version' ]]; then
- version
- graceful_exit
- fi
- if [[ "$1" = '--usage' ]]; then
- usage
- graceful_exit
- fi
- if [[ "$1" = '--list' ]]; then
- listAlarms
- graceful_exit
- fi
- # -----------------------------------------------------------------------
- # Verify that the usage function and the helptext function both have
- # documentation for each short option. Verify that the header comment
- # also has each short option listed in the usage section.
- # -----------------------------------------------------------------------
- # detect which environment is running and set the dialog program
- # appropriately
- if [[ "$(pidof ksmserver)" ]]; then
- if which kdialog >/dev/null 2>&1; then
- dialog=kdialog
- elif which zenity >/dev/null 2>&1; then
- dialog=zenity
- else
- error_exit "Please install kdialog"
- fi
- elif [[ "$(pidof gnome-session)" ]]; then
- if which zenity >/dev/null 2>&1; then
- dialog=zenity
- elif which kdialog >/dev/null 2>&1; then
- dialog=kdialog
- else
- error_exit "Please install zenity"
- fi
- elif [[ "$(pidof xfce-mcs-manager)" ]]; then
- if which zenity >/dev/null 2>&1; then
- dialog=zenity
- elif which kdialog >/dev/null 2>&1; then
- dialog=kdialog
- else
- error_exit "Please install zenity"
- fi
- else
- if which kdialog >/dev/null 2>&1; then
- dialog=kdialog
- elif which zenity >/dev/null 2>&1; then
- dialog=zenity
- else
- error_exit "Please install kdialog or zenity"
- fi
- fi
- while getopts :hluVx opt; do
- case $opt in
- h)
- helptext
- graceful_exit
- ;;
- l)
- listAlarms
- graceful_exit
- ;;
- u)
- usage
- graceful_exit
- ;;
- V)
- version
- graceful_exit
- ;;
- x) # set debugging output on a per-run basis. The $DEBUG
- # environment variable will enable degugging output at
- # the top of the script.
- set -x
- ;;
- :) # process missing required parameters
- error_exit "-$OPTARG: required argument missing"
- ;;
- *) # process unknown options
- error "Unknown option: -$OPTARG"
- usage
- clean_up
- exit 1
- ;;
- esac
- done
- shift $[ $OPTIND - 1 ]
- # to create multiple files, give number as argument to function call. This
- # creates an array of file names starting at index 0 ($TEMP_FILE)
- make_temp_files 1
- case $dialog in
- kdialog)
- time=$(kdialog --inputbox 'Alarm time:' --title "${PROGNAME^}" 2>/dev/null)
- if [[ -z "$time" ]]; then
- graceful_exit
- fi
- case ${time,,} in
- noon)
- time=1200
- ;;
- midnight)
- time=0000
- ;;
- esac
- if ! date --date="$time" >/dev/null; then
- kdialog --msgbox 'Invalid date string' --title "${PROGNAME^}" 2/dev/null
- error_exit 'Invalid date string'
- fi
- alarmTime=$(date --date="$time" '+%-H:%M %b %-d')
- message=$(kdialog --inputbox 'Message:' --title "${PROGNAME^}" 2>/dev/null)
- if [[ -z "$message" ]]; then
- graceful_exit
- fi
- at $alarmTime <<EOF
- export DISPLAY=$DISPLAY
- kdialog --msgbox "$message" --title "${PROGNAME^}" 2>/dev/null
- EOF
- kdialog --msgbox "Alarm scheduled for: $alarmTime" --title "${PROGNAME^}" 2>/dev/null
- ;;
- zenity)
- time=$(zenity --entry --text='Alarm time:' --title=${PROGNAME^} 2>/dev/null)
- if [[ -z "$time" ]]; then
- graceful_exit
- fi
- case ${time,,} in
- noon)
- time=1200
- ;;
- midnight)
- time=0000
- ;;
- esac
- if ! date --date="$time" >/dev/null; then
- zenity --info --text='Invalid date string' --title=${PROGNAME^} 2>/dev/null
- error_exit 'Invalid date string'
- fi
- alarmTime=$(date --date="$time" '+%-H:%M %b %-d')
- message=$(zenity --entry --text="Message:" --title=${PROGNAME^} 2>/dev/null)
- if [[ -z "$message" ]]; then
- graceful_exit
- fi
- at $alarmTime <<EOF
- export DISPLAY=$DISPLAY
- zenity --info --text="$message" --title=${PROGNAME^} 2>/dev/null
- EOF
- zenity --info --text="Alarm scheduled for: $alarmTime" --title=${PROGNAME^} 2>/dev/null
- ;;
- *)
- error_exit 'no dialog utility found'
- ;;
- esac
- graceful_exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement