Shell Programming/example code using control statements

Simple alarm clock - example code using control statements
The following code assumes bash environment with dialog utility present. $1 denotes value of first parameter. Infinite until loop in code line 18 drains memory after long time.

#!/bin/bash #  # Simple alarm clock # Author:mmmooonnnsssttteeerrr # GPL released time=`date +%H%M%S` default=060000 quit=false alarm=$1 help="Usage: alarm [%HH%MM%SS | default]\nDefault set to $default." if [ -z "$alarm" ]; then echo -e "$help" exit 1 fi  if [ "$1" = "default" ]; then alarm=$default fi  until [ "$quit" = "true" ]; do       dialog --title "alarm2" --infobox "Current time(%H%M%S)=$time\nAlarm set at $alarm. Ctrl+c to exit." 4 40      sleep 1 time=`date +%H%M%S` if [ "$time" = "$alarm" ]; then snooze=true until [ snooze = false ]; do                  dialog --title "alarm2" --infobox "$alarm(%H%M%S) has arrived. Ctrl+c to exit." 3 70                  echo -ne "\a" sleep 1 done quit=true fi  done exit 0

Simple alarm clock2 - example code using control statements
This is yet another alarm program. This time code is more efficient because it uses sleep command instead of infinite loop, so there is less risk of memory drainage. The trap command at code line 7 traps signal INT (ctrl-c) and performs cleanUp function as after action. cleanUp { exit 1 } trap cleanUp INT help="Usage: alarm3 [%H] [%M]" if [ -z $2 ]; then echo $help exit 1 fi time=`date +%H` alarm=$1 sleepfor="" if [ $alarm -lt $time ]; then sleepfor=`expr 24 - $time + $alarm` sleepfor=`expr $sleepfor "*" 60` else sleepfor=`expr $alarm - $time` sleepfor=`expr $sleepfor "*" 60` fi time=`date +%M` alarm=$2 if [ $alarm -lt $time ]; then sleepfor=`expr $(($sleepfor + 60 - $time + $alarm)) "*" 60` else sleepfor=`expr $(($sleepfor + $alarm - $time)) "*" 60` fi sleep $sleepfor soundAlarm { for beep in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 do                [ $beep = 20 ] &&  break echo -ne "\a" sleep 1 done } soundAlarm exit 0
 * 1) !/bin/bash
 * 2) Author: mmmooonnnsssttteeerrr
 * 3) GPL
 * 1) Have to calculate how long to sleep.
 * 2) Hours
 * 1) Minutes

Simple alarm clock 3
Actually, this code is the only one that does a reasonable job as an alarm clock. It is based on the following algorithm: If a_h=T_h then (a_h-T_h)+a_m-T_m where a_h is alarm_hour, T_h is Time_hour, a_m is alarm_minute, T_m is Time_minute.

# # # alarm_hour=$1 alarm_min=$2 alarm_min=`expr $alarm_min "*" 60` time_hour=`date +%H` time_min=`date +%M` time_min=`expr $time_min "*" 60` if [ -z $2 ]; then echo "alarm [%H] [%M]" exit 1 fi trap `exit 1` INT if [ $alarm_hour -lt $time_hour ]; then x=`expr $((24-$time_hour+$alarm_hour)) "*" 3600` sleepfor=`expr $x + $alarm_min - $time_min` else y=`expr $((alarm_hour-time_hour)) "*" 3600` sleepfor=`expr $y + $alarm_min - $time_min` fi echo $sleepfor sleep $sleepfor for beep in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 do       [ $beep = 20 ] && break echo -ne "\a" sleep 1 done exit 0
 * 1) !/bin/bash
 * 1) Beeps at a given time accurate to +-1min
 * 2) mmmooonnnsssttteeerrr
 * 1) GPL