Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 04-13-2007, 02:37 AM   #1 (permalink)
Greg's Avatar
 
Indeed.

Join Date: Dec 2004

Posts: 1,554

Greg is on a distinguished road

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  
Old 04-17-2007, 11:56 AM   #2 (permalink)
Nick's Avatar
 
Courtesy of Mak.

Join Date: Feb 2007

Location: Chichester, England

Posts: 4,003

Nick is on a distinguished road

Send a message via MSN to Nick
Default Re: Another BASH Script

Quote:
Originally Posted by Greg View Post
. And who needs RAM, anyway?[/code]
its slightly important
__________________
Every time I get Rep, I die inside.

Cisco Cert.
Nick is offline  
Old 04-17-2007, 06:35 PM   #3 (permalink)
Greg's Avatar
 
Indeed.

Join Date: Dec 2004

Posts: 1,554

Greg is on a distinguished road

Default Re: Another BASH Script

meh, 640K ought to be enough for anybody.

Well since this thread is bumped anyway I might as well post the version that actually works :happy:

Code:
#!/bin/bash
MAIN ()
{
HOURD="`date +%l | tr -d [:blank:]`"
TIME="$HOURD:`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 ()
{
if [ "$ALMN" -gt "49" ];
then ALTIME="$[ALHR+1]:$[60-ALMN]:00 $ALAMPM"
else ALTIME="$ALHR:$[ALMN+10]:00 $ALAMPM"
fi
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
SETAMPM
}
SETAMPM()
{
echo -e -n "AM/PM: "
read ALAMPM2
ALAMPM=`echo $ALAMPM2 | tr a-z A-Z`
ALTIME="$ALHR:$ALMN:00 $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
There's still a few things wrong with it, but most of it works.
__________________
Greg is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On