Categories
coding

Fortune

I’ve been learning Terminal commands from a new book that I got, “Tweak Your Mac Terminal“.

Reading through the first chapter, I discovered a simple command-line utility called “Fortune”. When it’s installed and run at the command-line, Fortune displays random quotations or proverbs. It is often used to provide a humorous or thought-provoking message to users when they log in or open a terminal session. I noticed within the fortune manual (man fortune) that there were some offensive fortunes that had been relegated to only operating when one entered the -o flag (fortune -o) but then discovered that the offensive quotations had actually been removed altogether from the home-brew repository because, well, it includes explicitly racist, homophobic, ethnically insensitive and other offensive content that directly conflicts with the Homebrew’s code of conduct.

Curiosity killed the cat and I found them online. Needless to say, they are at least pretty crass if not outright offensive. But finding them led me down the path to discovering how one creates their own quotations to add to the library of possible fortunes.

To add your own quotations or messages to the fortune database, you’ll need to create a custom fortune file and then compile it into a .dat file. Here are the steps:

Create a Fortune File:
  • Open a text editor, such as bbedit, nano, vim, or gedit.
  • Add your quotations or messages, one per line, to the file. Each message should end with a % character. For example:
This is my first custom fortune message.
%
Here's another custom fortune message.
%
Add as many messages as you like.
%
  • Save the file with a .txt or .dat extension (e.g., my_fortunes.txt).
Convert the Fortune File:
  • Use the strfile command to convert your text file into a .dat file. The .dat file is used by the fortune program to provide random messages efficiently. Run the following command:
strfile my_fortunes.txt my_fortunes.dat
  • Replace my_fortunes.txt with the actual name of your text file and my_fortunes.dat with the desired name for your .dat file.
  • Install or Copy to Fortune Directory:
  • If you want to make your custom fortunes available system-wide, you can copy the .dat file to the system’s fortune directory. On most Unix-like systems, this directory is typically /usr/share/games/fortunes/ or /usr/local/share/games/fortunes/. You may need to use sudo to copy the file:
sudo cp my_fortunes.dat /usr/share/games/fortunes/