Wednesday, August 5, 2009

Fortune cookies

On some systems (i.e. Slackware) after logging to the system you can see funny sentences. The fortune command is responsible for that. This small utility displays random aphorisms from its database. Type fortune and you see something like that:

$ fortune
You can only live once but if you do it right, once is enough.

Run it several times and see various aphorisms. When run with -o switch it can show you only the potentially offensive ones. The -s switch selects only the short aphorisms and the -l switch selects the long ones.

All the epigrams are placed in several files (without .dat extension) in /usr/share/games/fortunes/. These files are normal text files. The sentences are separated with '%' sign, so you can easily look through them. If we want the command to get sentences only from specific file, we give name of this file as the first argument:

$ fortune startrek

will give us only quotes from the star trek.

Creating our own sentences files

We can easily create our own data file. First we have to create normal text file like this:

My first sentence
My second sentence
Good bye

Let's call it ourfortunes. Now we have to to create the .dat file - this is the binary file, which allows random access to all the sentences from our text file. We do this with strfile command:

$ strfile ourfortunes

The file ourfortunes.dat will be created. Now we have to move both files to /usr/share/games/fortunes. We can use our own fortunes:

$ fortune ourfortunes
Good bye

No comments:

Post a Comment