Jump to content

Shell Programming/example code using control statements

From Wikibooks, open books for an open world

Simple alarm clock - example code using control statements

[edit | edit source]

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

[edit | edit source]

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.

#!/bin/bash
# Author: mmmooonnnsssttteeerrr
# GPL
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=""
# Have to calculate how long to sleep.
# Hours
if [ $alarm -lt $time ]; then
        sleepfor=`expr 24 - $time + $alarm`
        sleepfor=`expr $sleepfor "*" 60`
else
        sleepfor=`expr $alarm - $time`
        sleepfor=`expr $sleepfor "*" 60`
fi
# Minutes
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

Simple alarm clock 3

[edit | edit source]

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 (24-T_h+a_h)+a_m-T_m
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.

#!/bin/bash
#
# Beeps at a given time accurate to +-1min
# mmmooonnnsssttteeerrr
#
# GPL
#
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