Screw your contrast

December 21, 2003

Fun with Fortunes

I love the fortunes program. I use it to add a random, humorous (usually) quote to my email signature. It greets me with a new quote every time I open a shell, and you can create your very own or download mine.

Signature

Most MUA’s use the contents of the file .signature to add the signature to each outgoing email. In order to append the fortune output to an email, we’re going to create a script that writes the output of the fortune program to the .signature file, and then add a crontab entry to ensure a random fortune for every email.

Open an editor, copy and paste the following code and save it as .siggen. You can actually save it as whatever name you want. The leading “.” makes the file hidden. You may omit it if you want, I prefer not to see it in my directory listing, you may want to. The name is, likewise, unimportant. Call it “scriptIgotfromthatgrimguy” for all I care, the contents of the file are what’s important.

#!/bin/sh
fortune > .signature

That’s all there is to it.

Now all you have to do is create a crontab entry that will call this script every so often and give you a new fortune signature for all of your outgoing email. In a terminal type crontab -e to open your crontab in “edit” mode. Add the following line to your crontab…

*/5 * * * * /home/grim/.siggen

And save the crontab (Ctrl-ZZ in Vi mode). This will update your signature file every five minutes. If you would like to change the frequency consult the man pages for crontab. (You really should learn how to use the crontab. You can use it to send youself email reminders for birthdays, oil changes and anniversarys, turn your computer into an alarm clock and automate other easily scripted tasks that must be performed at specific times).

Shell fortune

In order to have your (bash) shell add a fortune at each login and to every term you open, add the following code to your .bashrc

GREET=`/usr/games/fortune`
echo $GREET
echo \"\"

Create your own

Creating your own fortunes is really very easy. First you need to create a fortune file. Fire up a text editor and type in all of the fortunes that you want in your fortune file. Each entry should be separated by a percentage sign (%) on it’s own line. For example…

Your witty fortune saying
%
Another one of your witty fortunes
%
It’s hard to come up with a bunch of these off the top of your head so, you may want to take your time and add to the file as you think of them

Save this as “myfortune” (or whatever you wanna call it). Now, you need to create a “dat” file so the fortune program knows how to call your fortune. Type the following from a shell…

strfile -r myfortune myfortune.dat

The “-r” flag tells strfile to randomly order the fortune entries. That way you’ll get, you know, random fortunes. And then…

cp myfortune myfortune.dat /usr/share/games/fortunes && cd /usr/share/games/fortunes && ln -s myfortune myfortune.u8

Note: You will probably have to be root in order to accomplish the previous command.

That’s all there is to it. To check out the fruits of your labour, type fortune myfortune to see your words of wonder and wisdom appear before your very eyes.