From 21de041e0c23038f1b36623788637ca7fe7e3679 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 20 Nov 2010 15:22:10 -0500 Subject: [PATCH] Add Alarm post. --- posts/Alarm.mdwn | 13 ++++++ posts/Alarm/alarm.sh | 94 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 posts/Alarm.mdwn create mode 100755 posts/Alarm/alarm.sh diff --git a/posts/Alarm.mdwn b/posts/Alarm.mdwn new file mode 100644 index 0000000..bee02ff --- /dev/null +++ b/posts/Alarm.mdwn @@ -0,0 +1,13 @@ +[[alarm.sh]] is a [[bash]] script wrapping [at][] to minimize the +number of keystrokes required to set a simple alarm. This one +currently rings a bell and prints a message, but the possibilities are +endless... + +You will almost certainly have to adjust my ALARM_SOUND_CMD to play a +file that exists on your system. Try `locate *.wav` to see what's +already there, or grab something new :). + +[at]: http://packages.qa.debian.org/a/at.html + +[[!tag tags/bash]] +[[!tag tags/programming]] diff --git a/posts/Alarm/alarm.sh b/posts/Alarm/alarm.sh new file mode 100755 index 0000000..4a28008 --- /dev/null +++ b/posts/Alarm/alarm.sh @@ -0,0 +1,94 @@ +#!/bin/sh +# +# Copyright (C) 2008-2010, William Trevor King +# +# 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 . + +# Can't drag yourself away from your software? Set an alarm! +# +# Caveats: +# You'll have to keep your current terminal open until the alarm goes off, +# or the message woln't be printed to the screen. +# The sound will still play. +# +# Echoed text may not appear if the terminal is in curses mode (e.g. Emacs) +# +# Your message text cannot contain any shell-sensitive characters, +# since it gets expanded at least twice before AT sees it. +# +# Minute resolution, since that's the best `at` can do. +# +# Script is hugely larger than the actual code, +# but hopefully well commented inside and user friendly outside. :p + +# Alarms should make some noise. +# Adapt this command to generate something on your particular system. +# Googling `beep wav` worked pretty well for me, or record your own :). +ALARM_SOUND_CMD="aplay $HOME/Music/system/towerclo.wav" +ECHO="/bin/echo -e" # echo command must expand '\n' to newline + +Usage () +{ + ERROR=$1 + $ECHO "$ERROR" + $ECHO " +usage: alarm [some message] + +where + will be recognized by at (see man at). + 'some message' will be printed to the current terminal. + must be the first argument, so if your time string is +several words long, enclose it in quotes ('6:00 tomorrow') to keep the +shell from breaking it into multiple arguments." +} + +# If there is no time argument, print a usage string. +if [ $# -lt 1 ] +then + Usage 'Incorrect number of arguments' + return 1 +fi + +# Initialize AT_CMD, the command we will send AT, and read in arguments. +AT_CMD="" +TIME=$1 +shift +MESSAGE="$*" + +Append_cmd () # Add command. In case you want to add whistles to the bells ;) +{ + NEW_CMD=$1 + AT_CMD="$AT_CMD\n$NEW_CMD" +} + +Append_cmd "$ALARM_SOUND_CMD" + +if [ -n "$MESSAGE" ] # if there is a non-zero message string... +then # ... append an $ECHO cmmd. + TERMINAL_NAME=$(tty) + Append_cmd "$ECHO \"$MESSAGE\" > $TERMINAL_NAME" +fi + +if false +then # debugging printout + $ECHO "'$AT_CMD'" + $ECHO + $ECHO "at '$TIME'" +fi + +# send the command off to at. +$ECHO "$AT_CMD" | at "$TIME" || Usage 'Bad time?' + +# use `atq` to list outstanding jobs. +# use `at -c ` to view the script at executes for debugging. -- 2.26.2