Friday, May 5, 2017

Testing Fortunes

Hello everyone.  This is how you can set up your shell in OSX (Terminal) to give you a short fortune about testing every time you open a new one.  As a bonus, we'll have a cow say it.

1.  If you have not installed Homebrew on your Mac, do so now.

Homebrew is a terrific package manager for OSX.  The upshot is that you can easily and quickly install/uninstall tools, utilities, and other programs.

In a browser, navigate to https://brew.sh/ and follow the instructions.

2.  Install fortune and cowsay.

Now that brew is installed, in the Terminal run these two commands:

brew install fortune
brew install cowsay

Restart your Terminal, and try it out:

fortune

You should get a random fortune.  Now try this:

fortune | cowsay 

The | character is called a 'pipe', and it should be on the forward slash key of your keyboard.  It takes the output from the first command and 'pipes' it into the second command.
 __________________
< To err is humor. >
 ------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


3. Set up .bash_profile

Still in Terminal, travel to your home directory, called '~' (you can just do cd ~).  In your home directory, see if you already have a .bash_profile file:

ls -a

The '-a' tells the ls command to show all files, even the hidden stuff (the ones starting with a . )  If you already have a .bash_profile file, great; otherwise run this:

touch .bash_profile

If you run ls -a again, you'll now see an empty .bash_profile file.  Now, we are going to use a built-in editor called nano to edit the file:

nano .bash_profile

If there is anything in the file, go to the last line and add a new line.  Enter this:

fortune | cowsay

Now, a CTRL-O to Write 'O'ut the file (save it as the same name), and CTRL-X to e'X'it.  You can now try it out by opening a new shell, or you can use this command:

source .bash_profile

4.  Add the testing.dat file

With your .bash_profile in place, go to the Finder.  Using the Go menu (or SHFT-CMD-G), go to the folder /usr/local/Cellar/fortune/9708/share/games .  (If the number is something other than 9708, you can figure it out.)  Take a look in the fortunes folder - all the .dat files are files of fortunes, which the fortune command will randomly draw from.  You can force a particular one to be used, like this:

fortune startrek

Go to http://noisycrow.com/files/testing.zip to get the file testing.zip.  It should unpack into:

testing
testing.dat

Save these into the fortunes folder from above.  You may have to change their permissions, which you can do in the Terminal:

cd /usr/local/Cellar/fortune/9708/share/games/fortunes
chmod 644 testing*

You can check the perms with:

ls -l testing*

The beginning of the lines should be "-rw-r--r--", which means that you will be able to 'r'ead the file. With that in place and the correct permissions, you should be able to try it out:

fortune testing | cowsay

5.  Modify your .bash_profile to use the testing fortune file.

So: back to your home directory (cd ~) and once again nano .bash_profile.  Change the fortune line to this:

fortune testing | cowsay

And CTRL-O (save) then CTRL-X (exit).  Now a new shell (or source .bash_profile), and you should get testing quotes from the cow:


 _________________________________________ 
/ “As ironic as it seems, the challenge \
| of a tester is to test as little as     |
| possible. Test less, but test           |
| smarter.”                             |
|                                         |
\ - Federico Toledo                       /
 ----------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
6.  Profit.

Well, not really.  But, you can play around with some of the stuff that fortune or cowsay do.  Try

man cowsay

to see what sort of options you have.  For example, try this:

fortune testing | cowsay -f moose

This is not limited to OSX - you can do it on pretty much any *NIX system, as long as you can find a copy of fortune for it, and then you'll need to figure out where the fortune files are stored, and so forth.  I have also done this for cygwin on windows.

You can make your own fortune files, or modify the testing one - fortune uses the "testing.dat" file, the "testing" file is the raw text.  If there is interest, I can detail how to do it.

Also, if you all have some good testing quotes out there, post them below.  If I like them, I'll add them to the dat file.  No guarantees though.

For extra credit: install and use ponysay instead.