i cant think A blog belong to Mohammad Azwan bin Ali. Most of his writings are about his real life story, ideas, thoughts, moderate political views, computer, programming, tips & tricks, particularly in Linux…. and of course it includes some random unspecified crappy stuffs too! Don't forget to check Movies and Junkyard pages out!
note: most of entries is not suitable for faint hearted person or anybody with iq below 100.

simple alarm clock with bash scripting

here is a simple bash programming that will execute alarm when it reach certain time setup by user

initially i coded just for exercise on bash scripting (practice make perfect right?) but i think it's a good idea to share it with other people

here's the code
#!/bin/bash
masa="`date +%k:%M`"
mp3="$HOME/Desktop/alarm.mp3" #change this


if [ $# != 1 ]; then
echo "please insert alarm time [24hours format]"
echo "example ./alarm 13:00 [will ring alarm at 1:00pm]"
exit;
fi
alarm=$1
if [[ "$alarm" =~ ^[0-2][0-9]\:[0-5][0-9]$ ]]
#fix me with better regex >_<
then
echo "time now $masa"
echo "alarm set to $alarm"
echo "will play $mp3"
else
echo "invalid clock format"
exit;
fi


while [ $masa != $alarm ];do
masa="`date +%k:%M`" #update time
done
echo $masa
if [ $masa = $alarm ];then
echo ringggggggg
play $mp3 > /dev/null 2> /dev/null &
fi
exit

if you have any suggestion/comment to improve this code please tell me :D

thanks

view proper script alarm bash script

note : you will need sox to use "play" command, debian/ubuntu user can install it by sudo apt-get install sox


7 Comments

[...] je bunyi dia. Mana la tau selepas habis saja saya belajar menulis shell scripts ni, saya dapat buat Alarm Clock yang lebih advanced dari saudara Novatech. January 16th, 2008Artikel Ubuntu Transparent Ubuntu [...]

Posted by Belajar Linux Command dan Shell Scripts di Ubuntu Newbie Malaysia on 16 January 2008 @ 8am

Maria Ozawa mesti tax boleh tinggal nyer.. wakakaka

Posted by fr33mumia on 17 January 2008 @ 2pm

Hi! I've found a mistake.
Istead of this line:
ifs [[ "$alarm" =~ ^[0-2][0-9]\:[0-5][0-9]$ ]]
writhe this:
if [[ "$alarm" =~ ^[1-2]?[0-9]\:[0-5][0-9]$ ]]

The timeformat is not 09:00. Tts 9:00
Sorry for my bad english.

Posted by Skully on 16 March 2008 @ 5pm

Hi! I've found a mistake.
Istead of this line:
ifs [[ "$alarm" =~ ^[0-2][0-9]\:[0-5][0-9]$ ]]
writhe this:
if [[ "$alarm" =~ ^[1-2]?[0-9]\:[0-5][0-9]$ ]]

The timeformat is not 09:00. Its 9:00
Sorry for my bad english.

Posted by Skully on 16 March 2008 @ 5pm

thnx for that fix [thumb], but i think the for format should be [0-2] since it's 00:00 when 12a.m but i wont effect much i believe nobody will set alarm for 12:00 am

Posted by novatech on 16 March 2008 @ 10pm

ive tested with 12 am.
http://tefeo.extra.hu/content/picture.png
the picture is very talkative.

Posted by Skully on 18 March 2008 @ 4pm

Nice script - just what I am looking for :)
But 100% cpu is not fine. Better to write:

while [ $masa != $alarm ];do
sleep 1 #wait 1 second
masa=”`date +%k:%M`” #update time
done

Posted by TippiX on 6 July 2008 @ 9pm