How can I configure cowsay?

cowsay command line with random cowfile:

fortune | cowsay -f `ls /usr/share/cowsay/cows/ | shuf -n 1`

Result:

     ___________________________________
/ Q: Who cuts the grass on Walton's \
\ Mountain? A: Lawn Boy.            /
 -----------------------------------
  \
   \ ,   _ ___.--'''`--''//-,-_--_.
      \`"' ` || \\ \ \\/ / // / ,-\\`,_
     /'`  \ \ || Y  | \|/ / // / - |__ `-,
    /@"\  ` \ `\ |  | ||/ // | \/  \  `-._`-,_.,
   /  _.-. `.-\,___/\ _/|_/_\_\/|_/ |     `-._._)
   `-'``/  /  |  // \__/\__  /  \__/ \
        `-'  /-\/  | -|   \__ \   |-' |
          __/\ / _/ \/ __,-'   ) ,' _|'
         (((__/(((_.' ((___..-'((__,'

You can add this command line to end of your login profile script for seeing random cowsay in any terminal. For bash, you would need to put this line in ~/.bashrc.


To make it also randomly choose between cowsay and cowthink, you can do this:

fortune | `ls /usr/games/cow* | shuf -n 1` -f `ls /usr/share/cowsay/cows/ | shuf -n 1`

This allows to have a one-line solution with random cowfiles, instead of painstakingly extending the Linux Mint script.


Well, in Linux Mint there is a fun thing you could do: write a script to select a cow and display a message from fortune. I'll get to it later. Once you have the script, all you have to do is to execute it. As suggested before, edit your ~/.bashrc file and add at the end a line containing the path to your script. For example, if you have the script in your home folder and the script's filename is "cowscript", then you could add the following line at the end of your ~/.bashrc file:

$HOME/cowscript

Now, the script used in Linux Mint 9 is the following:

#!/bin/bash
RANGE=4

number=$RANDOM
let "number %= $RANGE"
case $number in
    0)
        cow="small"
        ;;
    1)
        cow="tux"
        ;;
    2)
        cow="koala"
        ;;
    3)
        cow="moose"
        ;;
esac

RANGE=2
number=$RANDOM
let "number %= $RANGE"
case $number in
    0)
        command="/usr/games/cowsay"
        ;;
    1)
        command="/usr/games/cowthink"
        ;;
esac
/usr/games/fortune | $command -f $cow

Basically, it will display a random cow (either small, tux, koala, or moose) and the message will be taken from fortune. Also, this script will execute wither cowsay or cowthink, the only difference being that cowthink will display a thinking cow instead of a talking cow.

Now, the fun thing is that you can modify the script to show more cows or to show different cows. To do that, you first need to know what cows you have installed. In a terminal, run:

cowsay -l

You can use any of those cows. All you have to do is to edit the script: if you want to add a new cow, just copy the lines containing "cow" (plus the number and semi-colons) and paste them before the line that says "esac". Then, change the number and name of the cow, so for instance, if you want to add a cow called "udder", all you have to do is to add these lines before the first "esac":

4)
    cow="udder"
    ;;

Important: the second line of the file, "RANGE=4", must be changed also. If you add one cow, then instead of 4 use 5, also if you delete one cow, then you must use 3, and so on. Also note that the numbers that you see must be in the range from 0 to RANGE - 1. That's why RANGE is 4 and the numbers are 0, 1, 2, and 3.

You could also create your own cows, although that might take a bit more of work. All you have to do is to get any ASCII art you like and edit it, but it is a bit tricky. You can see how it's done here: http://lmpeiris.wordpress.com/2011/01/17/cowsayhow-to-make-a-cow-talk-on-terminal-startup/ However, consider that any @ and \ symbols need to be backslashed, that is, you must put before that symbol this other symbol: \. This might be the case for #, too (but not always). If your ASCII Art contains #, you could backslash it too, but sometimes it would be enough with just one of them... I'm not sure how to explain it, sorry. I guess you will have to try to see if it works. Also, make sure that the file you edit has the extension ".cow"

Finally, once you have your own cows, you can either add them to the default location (you probably will need to be superuser for that) at /usr/share/cowsay/cows, or you could add to your ~/.bashrc file this lines:

export COWPATH="/usr/share/cowsay/cows"
# set COWPATH so it includes user's cows
if [ -d "$HOME/cowfiles" ] ; then
    COWPATH="$COWPATH:$HOME/cowfiles"
fi

Be sure to add those lines before you call your "cowscript". This also assumes that you have a folder called "cowfiles" on your home folder. Change the path if you want so it points to the folder where you have your cowfiles.


If you want to combine cowsay and fortune to present you with a message each time you start a terminal, add the following line:

fortune | cowsay -f tux

to the file .bashrc in your home folder.