Home Code Other Software

Table of Contents

Introduction

fortune is yet another implementation of the Unix-style fortune program that displays a random message from a database of quotations. Conceptually, it’s similar to the BSD fortune program originally written by Ken Arnold. Unlike Arnold’s program, this version is written in Python and should run anywhere there’s a Python interpreter.

Usage

fortune [OPTIONS] [/path/to/fortunes]

Options

If the fortune file path is omitted, fortune looks for the path in the FORTUNE_FILE environment variable. If that environment variable isn’t set, fortune aborts with an error.

Like Arnold’s fortune program, this version uses a database of fortunes (the fortune cookie database) generated from a text file. The text file consists of possible multi-line quotes, separated by lines consisting of a single ”%” character. For example:

Don't go around saying the world owes you a living.  The world owes you
nothing.  It was here first.
        -- Mark Twain
%
Every normal man must be tempted at times to spit on his hands, hoist the
black flag, and begin slitting throats.
        -- H.L. Mencken
%
Behind every argument is someone's ignorance.
        -- Louis Brandeis

For efficiency and speed, fortune uses an index file to record the location of each fortune in the database. So, before you can use fortune with a new (or modified) fortune cookie database, you must generate the index. Unlike Arnold’s version of fortune, you use the fortune program itself to generate the database, instead of a separate strfile program. For example, if your fortune cookies are in a file called fortunes, you generate the database with this command:

$ fortune -u fortunes

This command will generate a binary fortunes.dat index file.

You should run fortune -u whenever you change your fortune cookie file.

As noted above, if the fortune file path is omitted, fortune looks for the path in the FORTUNE_FILE environment variable. If that environment variable isn’t set, fortune aborts with an error.

Displaying fortunes

Once you have an index file, you can generate a random fortune simply by running the fortune with the path to your text fortunes file:

fortune /path/to/fortunes

Again, as noted above, if the fortune file path is omitted, fortune looks for the path in the FORTUNE_FILE environment variable. If that environment variable isn’t set, fortune aborts with an error.

Getting and installing fortune

Installing via pip

Because fortune is available via PyPI, if you have pip installed on your system, installing fortune is as easy as running this command (usually as root or the system administrator):

pip install fortune

Installing from source

You can also install fortune from source. Either download the source (as a zip or tarball) from http://github.com/bmc/fortune/downloads, or you can make a local read-only clone of the Git repository using one of the following commands:

$ git clone git://github.com/bmc/fortune.git
$ git clone http://github.com/bmc/fortune.git

If you don’t have git, you can download the source distribution, as a zipfile or a tarball, from the Git repository.

Once you have a local fortune source directory, change your working directory to the source directory, and type:

python setup.py install

To install it somewhere other than the default location (such as in your home directory) type:

python setup.py install --prefix=$HOME

Differences from the BSD fortune program

This version of fortune does not provide some of the more advanced capabilities of the original BSD program. For instance, it lacks:

It does, however, provide the most important function: The ability to display a random quote from a set of quotes.

I have a fortune cookie file that contains more than 2,800 fortunes I’ve collected, from various sources, over the last 25 years. Feel free to download it from http://github.com/bmc/fortunes/.

Author

Brian M. Clapper, bmc@clapper.org

Copyright © 2008-2012 Brian M. Clapper

License

BSD license. See accompanying license file.