User avatar
LemmeFatale
Posts: 253
Joined: Fri Feb 01, 2013 8:47 pm
Location: UK

Setting up fortune and Adding Custom Fortune Files: A Guide

Mon Sep 09, 2013 12:15 am

I found myself missing the command line application, "fortune", on my Raspberry Pi, running Raspbian. Not only that, I wanted to add fortunes of my own to it, too, so I learned how, and decided to write a guide as I went about setting it up - and now I'm sharing that guide here. Some screenshots are included, showing what I did.

For those who don't know what fortune is, it's really quite simple: Once it's been installed, if you run the command fortune at a terminal, it will display a random piece of text from a file - a "fortune file" - containing lots of such pieces of text. The name and concept, of course, refer to fortune cookies - those delicious biscuits with a piece of paper inside containing a random idiom or one-liner. It's just a random bit of fun. It debuted in Version 7 Unix, meaning that it dates all the way back to 1979! More information about fortune is available here.

If you would like to put this on your own Raspberry Pi, and add your own collection of witticisms to it, here's how.

1: First, you need to actually install the program on Raspbian. To do this, run the following command at the command-line;

Code: Select all

sudo aptitude install fortune-mod
This tells aptitude* to install "fortune-mod", which contains the program fortune and a selection of fortune files to go with it.

*(The program aptitude is a relation of apt-get, which I personally prefer to use. You can change the command to use apt-get instead, if you prefer that, but it does the same thing as far as this guide is concerned.)

Image

At this point, I must give credit to the guide from which I learned how to create custom fortune files. That guide is located here, and the next few instructions are based on it.

2: This next step is the fun part: Actually making your fortune file! This is very simple - you simply open up your favourite Linux text editor (for example, you could use nano at the terminal, or whatever your preference is in your desktop environment of choice), and write whatever you want.

It is important to separate each quote with a new line containing a percent sign - % - this is what tells fortune that your different pieces of text are separate quotes. To demonstrate this, a small sample taken from my own custom fortune file is below;
And it shall pass that the reviewer can't get off level one. And he shall not care for the game, or want to play it for more than 20 minutes. But lo, he shall be fearful of the software company dropping advertising, and missing him off the free crate of beer list at Christmas, and he shall tremble and quake mightily as all rival mags have given it a high score. So he shall give it a score low enough to dissuade potential buyers, yet high enough to pacify the software people. And that score shall be the NUMBER OF THE BEAST, and it shall be 73 percent.
%
The computer hobbyist spirit is almost dead. Perhaps what is really wrong here is a lack of curiosity or unwillingness to experiment. ☯93MAR

wwwtxt.org
%
The game Bubble Bobble is out for the IBM PC. The version I have is in EGA and only uses the PC-SPEAKER. However, it is quite fun. ☯93FEB

wwwtxt.org
%
Please don’t reply to this message unless you detect a fault somewhere. ☯93SEP

wwwtxt.org
%
When you're happy with your fortune file, save it and give it a memorable name. DO NOT give this file an extension, as it will cause it not to work later - for example, myfortunes.txt is no good, but myfortunes is fine.

Don't use the name fortunes, as this is the name of one of the fortune files included with fortune-mod.

I would suggest saving the file to your home directory (the one named after your username), as that will mean less typing later on.

If you would prefer not to type your own fortune file (but where's the fun in that? ;) ), you could always grab a ready-made one from here, instead.

3: When your fortune file is finished and saved, you will need to run the following commands - just remember to change the name written in the command to whatever you called your fortune file (for the sake of this example, we'll just say that it's called myfortunes).

Code: Select all

strfile -c % myfortunes myfortunes.dat
My understanding is that this creates a database to go with your text file, which allows fortune to randomly display quotes from it when you run the program.

Image

Image

4: You now need to run this command at the command-line - just remember to change the name of the home directory, the fortune file, and the .dat file to match what yours are;

Code: Select all

sudo cp /home/yourname/myfortunes /home/yourname/myfortunes.dat /usr/share/games/fortunes/
This will copy your fortune file and the matching .dat file into the directory where fortune reads fortune files from.

Image

If you want to keep the supplied fortune files and have your own quotes mixed in with them, you can stop here. Simply run the following a few times (until you see one of your own quotes) to test it;

Code: Select all

fortune
If you want to remove the provided fortune files so that only your own are displayed, there's one more step to follow...

5: The image below shows the now-current state of the directory where the fortune files are kept - it has my custom fortune file in it, and the three included ones, too.

Image

To remove the fortune files that were installed with fortune-mod, run the following command;

Code: Select all

sudo rm /usr/share/games/fortunes/fortunes /usr/share/games/fortunes/fortunes.dat /usr/share/games/fortunes/fortunes.u8 /usr/share/games/fortunes/literature /usr/share/games/fortunes/literature.dat /usr/share/games/fortunes/literature.u8 /usr/share/games/fortunes/riddles /usr/share/games/fortunes/riddles.dat /usr/share/games/fortunes/riddles.u8
This will remove the files fortunes, fortunes.dat, fortunes.u8, literature, literature.dat, literature.u8, riddles, riddles.dat, and riddles.u8 from the directory /usr/share/games/fortunes/ leaving only your custom fortune file for fortune to read.

And that's it! Run fortune now, and you'll be able to see your own quotes randomly shown on-screen, on-demand.

Image

(Please excuse the broken word-wrapping in the picture above - I haven't yet learned how to fix this issue!)

Some folks like to use fortune in conjunction with another program called cowsay, for humour and/or novelty value, but I'll leave that for curious readers to learn about on their own, as it's outside of the scope of this guide.

Note that, if you change your fortune file, you'll need to run through the steps to create the .dat file and, if necessary, to put the fortune file and the .dat file in the right place, again.

I hope that this has been helpful to anyone wanting to display random text, at random, on Raspbian!
Classic - Raspberry Pi Model B (512MB) with Motorola Atrix Lapdock
Lemcon-One - Raspberry Pi Model B (256MB) PiMAME TV-Box

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Setting up fortune and Adding Custom Fortune Files: A Gu

Mon Sep 09, 2013 1:08 am

Thanks! This was very interesting and informative.

Yes, I always thought the "fortune" command (and database) was an essential part of any Unix distribution. Fancy the idiocy of them leaving it out in Raspbian...
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
LemmeFatale
Posts: 253
Joined: Fri Feb 01, 2013 8:47 pm
Location: UK

Re: Setting up fortune and Adding Custom Fortune Files: A Gu

Mon Sep 09, 2013 3:17 pm

Thanks!

If I could get hold of the Tip of the Day list from the homepage, I'd compile that into a fortune file. Maybe a list of amusing (computing-related, to give it a focus) one-liners from the forum, too. :lol:
Classic - Raspberry Pi Model B (512MB) with Motorola Atrix Lapdock
Lemcon-One - Raspberry Pi Model B (256MB) PiMAME TV-Box

User avatar
daniel_rehn
Posts: 1
Joined: Sun Jul 06, 2014 8:16 am
Contact: Website

Re: Setting up fortune and Adding Custom Fortune Files: A Gu

Sun Jul 06, 2014 8:26 am

Hey LemmeFatale! I just wanted to say thanks for creating a “fortune” guide and for the http://wwwtxt.org shout-out. (I'm the human who posts those messages!) Super cool! :P

Return to “General discussion”