View Single Post
Old 04-13-2007, 02:37 AM   #1 (permalink)
Greg
Indeed.
 
Greg's Avatar
 
Join Date: Dec 2004
Posts: 1,533
Talking Another BASH Script

Whenever I am on the computer I always forget to get off to do something at a certain time, so I decided I would write this alarm clock BASH script. There's probably already a program to do this, but I had some spare time tonight and I would like to improve my sucky BASH skills. I realize this might not be the most efficient way to do it (it uses up more and more memory as it goes on ), but it gets the job done. And who needs RAM, anyway?

Code:
#!/bin/bash
MAIN ()
{
TIME="`date +%l`:`date +%M`:`date +%S` `date +%p`"
MAINOP="DEFAULT"
clear
if [ "$TIME" == "$ALTIME" ];
then {
RING ()
{
clear
echo -e -n "\a"
sleep 0.1
echo -e -n "\a"
sleep 0.1
echo -e -n "\a"
sleep 0.1
echo -e -n "\a"
sleep 0.1
echo -e -n "\a"
sleep 0.1
echo -e -n "\a"
sleep 0.1
WAIT
}
SLEEP ()
{
ALTIME="$ALHR:$[ALMN+10]:$ALSC $ALAMPM"
MAIN
}
WAIT ()
{
DISMISS=""
read -t 5 -n 1 -p "ALARM - Strike D to Dismiss or S to Sleep." DISMISS
if [ "$DISMISS" == "d" ];
then {
RESETHOUR
MAIN
}
elif [ "$DISMISS" == "s" ];
then SLEEP
else RING
fi
}
RING
}
fi
echo -e "\nTime  - $TIME"
if [ "$ALTIME" == "FOO" ];
then echo "Alarm - (Unset)"
else echo "Alarm - $ALTIME"
fi
echo -e "\n1: Set Alarm\n2: Reset Alarm\n3: Exit"
STTY_ORIG=`stty -g`
stty -echo
read -n 1 -t 1 MAINOP
stty $STTY_ORIG
if [ "$MAINOP" == "1" ];
then SETHOUR
elif [ "$MAINOP" == "2" ];
then RESETHOUR
elif [ "$MAINOP" == "3" ];
then clear && exit
else MAIN
fi
}
RESETHOUR ()
{
ALTIME="FOO"
MAIN
}
SETHOUR ()
{
clear
echo -e -n "\nHour: "
read ALHR
if [ "$ALHR" -gt "12" ];
then {
echo -e "Invalid hour.\n"
SETHOUR
}
fi
SETMIN
}
SETMIN()
{
echo -n "Minute: "
read ALMN
if [ "$ALMN" -gt "59" ];
then {
echo -e "Invalid minute.\n"
SETMIN
}
fi
SETSEC
}
SETSEC()
{
echo -e -n "Second: "
read ALSC
if [ "$ALSC" -gt "59" ];
then {
echo -e "Invalid second.\n"
SETSEC
}
fi
SETAMPM
}
SETAMPM()
{
echo -e -n "AM/PM: "
read ALAMPM2
ALAMPM=`echo $ALAMPM2 | tr a-z A-Z`
ALTIME="$ALHR:$ALMN:$ALSC $ALAMPM"
if [ "$ALAMPM" == "PM" ];
then echo -e "\n"
elif [ "$ALAMPM" == "AM" ];
then echo -e "\n"
else echo "Invalid input." && SETAMPM
fi
SETDONE
}
SETDONE()
{
echo -e "Alarm set for $ALTIME"
read -n 1 -p "Press any key to continue."
MAIN
}
ALTIME="FOO"
MAIN

__________________
Greg is offline   Reply With Quote