Because FLOSS is handy, isn’t it?

Do one thing, do it well. Part II

Posted by isilanes on September 19, 2006

This post is the continuation of a previous one. In that first part I mentioned the “bells and whistles” of most Windows programs being even counterproductive, and getting in the way of the user. Here I will elaborate on that.

Most GNU and Free Software applications for GNU/Linux and other Unix-like systems have the possibility of being called from the command line, appart from any GUI they may have.

One would think that no-one in his right mind would use an ugly CLI, where a visual point-and-click GUI exists. However, CLIs give a substantial flexibility, and I will illustrate this with an example.

Suppose you want your computer to remind you of the upcoming birthdays of your friends. Suppose you use Windows. Then, you have to find a program like Birthday Reminder Plus 2006, or whatever, which does it. Maybe you want to be reminded by e-mail… but you will have to make do with what the options in the BRP2006 GUI give you. If BRP2006 has a menu with: “Remind with a beep” and “Remind with a pop-up”, you will have to choose one, and that’s it. Windows programs don’t expect you to think or develop. They expect you to use Google, eMule and so on to download a pirated copy of a monolithic program that fits your needs. If BRP2006 has not an option for reminding you via e-mail, then you have to drop it alltogether, and keep searching for Birthday MegaReminder for PowerUsers 2007, which has such an option.

With GNU/Linux, maybe there is such a program, and you are free to use it. However, there are far better solutions, which you can tailor to your needs. The following is what I actually do to be reminded of birthdays:

First, I need a kind of “database” of birthdays, and a program that, reading this database, can extract the upcoming ones (the ones for which a given amount of days or less, are left). There are probably many of them, but I use one called simply Birthday. I haven’t found the Home Page of this program (to give it here), but I use the Debian package, mantained by Alexander Neumann. The “database” consists in a file called .birthdays that we have to place in our home/ directory. This file will contain lines like:

Bill Gates=30/09/666

Then, when called from the command line, it will output:

Bart[~]: birthday
Bill Gates is 1340 years old in 4 days' time.

If no option is given, birthdays in the following 21 days will be printed out. If you want to see birthdays in the following X days, just issue birthday -W X.

Second, this output is ugly. For example, the use of “is” is wrong, and “days’ time” could be shortened to “days”. To do so, we can pipe it through sed (another GNU utility):

Bart[~]: birthday | sed -e "s/ is / will be /;s/'.*//g"
Bill Gates will be 1340 years old in 4 days

Thus, sed substitutes ‘ is ‘ with ‘ will be ‘, and a literal ‘ and anything behind it ('.*) with nothing (effectively deleting it).

Third, we have to send this “Bill Gates will be…” to our e-mail address. For that, we will use the mail GNU command, like this:

Bart[~/]: birthday | sed -e "s/ is / will be /;s/'.*//g" | mail -s '[BIRTHDAY]' myaddress@myisp.org

This will send an e-mail to myaddress@myisp.org, with the subject [BIRTHDAY], and the body being the Bill Gates will be 1340 years old in 4 days text above.

Fourth, we need to automate this. For that, we can make a little Perl script:

#!/usr/bin/perl -w

# This scripts e-mails me to remind me of
# upcoming birthdays. It needs the ‘birthday’
# package.

use strict;

# Test if birthday package is installed:
die “No birthday package!\n” if (`which birthday` =~ /not found/);

# Read the list of upcoming birthdays, formated with sed:
my @bulk = `birthday | sed -e “s/ is / will be /;s/’.*//g”`;

# The e-mail address:
my $u = ‘a@b.c’;

# Send e-mail, if there is any upcoming birthday:
system “echo ‘ @bulk’ | mail -s ‘[BIRTHDAY]’ $u” if @bulk;

Next, we have to set this script to run periodically. We can do it with the GNU cron utility. This is a daemon, constantly running in the background (if its service has been activated), wasting negligible resources, and executing the task the users schedule, via the crontab command, or even a GUI like Kcron.

So, I use kcron to schedule the Perl script above to run everyday, at 7:01 am, et voilà! I receive an e-mail from my system every morning, reminding me of the upcoming birthdays.

Uf, all THAT has to be done?

OK, it sounds like a lot of work, for something our Birthday MegaReminder 2007 could do with half a dozen clicks.

Now, consider. The procedure outlined here is fully modular. Any time you want to modify something, you can act on the relevant step: e.g.: if you want to add a birth date to your list, you can just add it editing the ~/.birthday file. If you want to be reminded just once a week, the crontab should be modified. If you want to be reminded 73 days in advance, just add -W 73 to the birthday command in the Perl script. If you want the reminder to be sent to a list of e-mails, instead of only to yourself, you can modify the script accordingly.

The good part is that now you have learned how to use different tools, for different tasks: You have used cron here, but you can use elsewere to schedule ANY job. You have used mail here, but you can use it elsewere to send any text by e-mail, including the contents of a file, the output of a command, or a fixed text.

Maybe the Birthday MegaReminder has scheduling capabilities (like cron), running in the background and activating itself when appropriate. That’s fine but… can its scheduling capabilities be used for other programs? Sorry, no. If you want a periodic scheduler for program X, you have to buy, or pirate, a copy of a program which not only makes X, but also has scheduling capabilities. Maybe Birthday MegaReminder has e-mailing capabilities (like mail), but… can they be used to e-mail other things? Sorry, no.

With the GNU tools (like cron, sed and mail), when I want a program to connect to the Web, retrieve my horoscope, and send a copy to my e-mail, I don’t need a program that is good at sending e-mails or scheduling actions. I need a program that is good at retrieving horoscopes from the Internet. Then, I have tools to e-mail me with the output of such a program, and/or schedule its execution periodically.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: