Is this the "right" way to make an alarmclock?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is this the "right" way to make an alarmclock?
# 1  
Old 09-22-2011
Is this the "right" way to make an alarm clock?

I'm pretty unsophisticated in my scripting. I never followed a method
to learn, I just use google when I want to figure out how to use a
command or preform a certain function (and most of the results are from
here or the ABSG). Anyway, there are a lot of gaps in my knowledge.

So, I wanted to write a simple, multi-purpose alarm-clock. What
I've got is tiny, and it works. It reads the user-specified time and command, and
executes the command at the time then exits. Here it is:

Code:
#!/bin/bash

time="$1"
command="$2"
trig=0

while [ $trig = 0 ]; do
	if [ "$(date +%R)" = $time  ]; then
		$command
		trig=1
	else
		sleep 5
		
	fi
done
exit

So I can give it a line like...

alarm 07:00 'mplayer annoying-sound.ogg'

... and it plays the annoying sound at 7:00.

Then I checked google to see how others had created alarms. They were
all rather involved, calling functions and using all sorts of commands I
didn't recognize, and all substantially longer.

Is there any reason my rather humble implementation is undesirable or
problematic?

Last edited by ninjaaron; 09-22-2011 at 07:09 AM..
# 2  
Old 09-22-2011
For a simple alarm it's enough. What others probably have added is plausibility checks for the time, DoW-variant alarm times, checking if the command line is correct, ...
# 3  
Old 09-22-2011
What's DoW-varient?
# 4  
Old 09-22-2011
Have you looked at the at daemon?
# 5  
Old 09-22-2011
DoW == Day of Week. Meaning the possibility to set different alarm times based on the day (wouldn't want the alarm to ring on 06:00 on a Saturday since I just went to bed Smilie )

@Skrynesaver: at does basically the same. But I think for someone who's just learning the hang of scripting, this is a good starting project, as it can be extended over time.
# 6  
Old 09-22-2011
Hmmm... this "at" looks a little complicated, but useful. I'll have to play with it a little.

On first glance, it looks like the syntax is better suited to scripts than terminal. Is that correct?

---------- Post updated at 02:32 PM ---------- Previous update was at 02:13 PM ----------

Quote:
Originally Posted by pludi
DoW == Day of Week. Meaning the possibility to set different alarm times based on the day (wouldn't want the alarm to ring on 06:00 on a Saturday since I just went to bed Smilie )
Ah. right. I was thinking about (among other things), calling it from another script with config files to organize calender events and things like that, and letting the parent script handle day of the week stuff.

Quote:
@Skrynesaver: at does basically the same. But I think for someone who's just learning the hang of scripting, this is a good starting project, as it can be extended over time.
I woudn't exactly say I'm 'just learning the hang of it.' I'm somewhere in the "upper-beginner/lower intermediate" level of bash (though bash is just a doorway to other things, I guess). I've written scores of them. My .xinitrc and .bashrc are from scratch. I've written some scripts that search web websites for correct files to download automatically, or create new directory strutures based on file types. I actually even wrote a system of 20 or so scripts to coordinated a bunch of UI elements to get a good system going on my netvertible with config files and everything. I can write scripts that do all kinds of cool things.

I'm just always insecure about whether I'm doing it the best possible way. For a while, I called all of my command substitutions with `substitution` syntax, and I would run into these weird problems with no apparent solution, and then I randomly stumbled across $(substitution) syntax and figured out how much better it is.

And I still need to learn sed. It looks so awesome for making config files, but I'm intimidated by the size of the man page. Smilie

Last edited by ninjaaron; 09-22-2011 at 08:42 AM..
# 7  
Old 09-22-2011
I didn't target that comment specifically at you, but more as a general statement for any "newbies" reading this thread.

And IMO I wouldn't parse config files with sed unless forced, as I think it's too much of a hassle if you can just source the file & use the variables from there. It's useful if you want to do some pre-processing on it (eg. if you want to use some special variable substitution or similar), but that's about it in this case.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Make scipt except from "Y","y" and "yes" to take [Enter] as being "yes"

This is the script: #!/bin/sh if ; then rm -rf /usr/share/WallpaperChanger; fi if ; then rm -rf /usr/bin/wallch; fi; if ; then rm -rf /usr/share/applications/wallch.desktop; fi if ; then rm -rf /usr/share/doc/wallch; fi if ; then rm -rf /usr/share/man/man1/wallch.1.gz; fi echo "Delete... (4 Replies)
Discussion started by: hakermania
4 Replies

7. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

8. Programming

how could i make a program mixed with many "|", "<" and ">"

I have written following code to do: ls -l | wc -w, it works: but when there are not only a single "|", if there are more such as: ls -l | sort -r | sort | sort -r, This program does not work, i want to know how could i deal with it when there are more "|", another situation is that, if it mixes... (2 Replies)
Discussion started by: strugglingman
2 Replies

9. UNIX for Dummies Questions & Answers

"./cofigure" and "make" turmoil

I am a Unix newbie and I downloaded some X11 programs. I have gcc, but I cannot compile the files. I NEED HELP!!!! :confused: (16 Replies)
Discussion started by: gnerd
16 Replies
Login or Register to Ask a Question