Alarm Script
I wanted to write something today, but I don't have access to my pictures right now. I realized that I haven't posted many of the scripts that I have written over the years. So, here's one I used to use for a long time before I created a proper GUI for it.
Background
I used to always forget my laundry when I was in college. I would put it in the washer or dryer, and I would completely lose track of time and never get it. I needed an alarm. My speakers weren't always on if I was doing homework, and I wasn't always looking at my monitor when I was wandering the apartment using the computer as a stereo. Therefore, I needed an alarm that included both audio and video. I opted for a video file.
Design
My next problem: I move stuff all over my computer all the time. I wanted to embed the video file. If I copied this to another computer, I knew I would forget an external file. I was also still trying to avoid writing a full-fledged program at the time. I wanted a quick script.
I nearly always work in a terminal in Gnome. So, rather than making the script take parameters, I decided that I would just call sleep
before my script. My script would call mplayer
because it is easy to use from the terminal and is my favorite video player.
Code
Here's what I wrote quickly for the script:
#!/bin/sh
tail -n +6 $0 | mplayer - >/dev/null 2>&1
exit 0
I should explain what this means. The first line is obligatory to a shell script.
The third line does multiple things. The tail
call opens a copy of file $0
, which is a variable set to the name of the script when the script is run. It strips the first five lines off i.e. starting at line 6, and it outputs that to STDOUT. mplayer -
opens mplayer, but rather than opening a file, it opens the stream from STDIN i.e. tail's STDOUT. >/dev/null 2>&1
redirects all of mplayer's output to /dev/null so it won't interfere with whatever I'm doing in the terminal at the time.
The fifth line exits the script so anything after it will not be executed.
I named my script "myalarm" and put it in my personal bin directory, which is mapped to my PATH. Finally I supplied it with a video file.
cat video.avi >>myalarm
I tested it, and it worked beautifully. Now, when I washed my clothes, I could wait 27 minutes by typing:
$ sleep 27m && myalarm &
Conclusion
I wish I had more uses for this kind of script. I've made self-exacting files, but that only saves me from having to write a "tar" command, which doesn't do much.
I was able to make this quickly and easily without needing to break out a compiler with resource files and GUI code. The amount of time you can save makes shell scripting and learning powerful one-liners worth it. I've saved myself hours of tedious file renaming, file duplication, and other long tasks by using one advanced bash command. It may seem dull to learn how shell commands and output redirection work, but it is one of the most important concepts to learn. You will learn all the specific commands you will need with time and man pages as you need them. I put Cygwin on any Windows machine I need solely for the power the *nix shell gives me.