Cowsay - How to Get Your Cow to Talk!
Note: In this article we will delve into the world of unix/linux. I use the word directory in this article. In the Microsoft Windows world we call them folders. Essentially, for our purpose, directories and folders are the same thing. Unix/linux types like to call them directories so we will stay with that convention.
Cowsay is a perl script created by Tony Monroe that generates ASCII pictures of a cow with a message.
I discovered cowsay way back in the early days of linux. It's a rather amusing little program.
The cows says, "Hello world!"
A library of cow files offer a variety of cows and other animals to select from.
As in the above image a cow can say a message or it can think a message (bubbles). A cow can also have different sates of mind:
- Borg mode
- Dead
- Greedy mode
- Paranoia
- Stoned
- Tired
- Wired
- Youthful
I thought it would be fun to get the cow to say something more then just text typed on a terminal command line.
Fortune is a program that displays a pseudorandom message from a database of quotations that first appeared in Version 7 Unix.
Fortune displayed on a terminal command line.
Hey, why not get the cow to say a fortune?... What frivolous nerd-filled fun!
With that I wrote a simple bash script to pipe a fortune to cowsay.
Along the way I discovered that I was not the only person having fun with this.
There are many ready to use bash scripts available on the Internet.
I have since re-wrote my original cowsay bash script. It now parses what cows are available on a system, checks against a list of exclusions and ensures that the caption will fit within the terminal window. Some cows are just too silly to display or too large for the terminal window.
Fortune piped to cowsay.
The script should now run on a variety of systems without a need for modification.
You can download the cowsay perl script, cowsay bash script and the cow library files here: cowsay.zip
I don't claim to be a programmer. I have not dedicate the time or training required to be proficient. So I hack away at it!
I'm sure there are better ways, better programming techniques to improve this script, however, it does work.
Constructive feedback is always welcome.
Installation and prepping your cow
Linux users
For those running a modern linux distribution you can install these programs through your distro's package manager:
- perl
- fortune-mod
- cows
- zip
- unzip
Debian, Ubuntu or Mint linux users can do this from the terminal.
Execute the following command:
sudo apt -y install perl fortune-mod cowsay zip unzip
Apple Mac users
You will need to install Homebrew or MacPorts. After installing Homebrew or MacPorts, install these programs:
- perl
- fortune-mod (fortunes)
- cowsay
- zip
- unzip
- coreutils
- bash
Your Homebrew/MacPorts version of bash may be located in directory: /usr/local/bin/bash or /opt/local/bin/
To find out where your bash shell lives, execute this command: type -a bash
If bash lives in /opt/local/bin/, edit the cowsay bash script and change the first line from:
#!/bin/bash
to
#!/opt/local/bin/bash
If bash lives in /usr/local/bin/, edit the cowsay bash script and change the first line from:
#!/bin/bash
to
#!/usr/local/bin/bash
Windows Users
For Windows users install Cygwin, along with the following Cygwin packages:
- perl
- perl-Text-CharWidth
- fortune-mod
- zip
- unzip
There is no cowsay package for cygwin so you will need to hunt one down or download my cowsay.zip archive from this article.
Note: for cygwin users there is no sudo command. You will need to open your cygwin terminal as Administrator:
Right mouse click on the "Cygwin64 Terminal" desktop icon and select "Run as administrator".
Launch Cygwin as Administrator.
In the cygwin terminal exclude the sudo command in the following installation instructions.
Cowsay archive
The cowsay.zip archive file from my website includes:
- cowsay/bin/cowsay Tony Monroe's perl script cowsay v3.03.
- cowsay/cows/ a directory of 78 cow files.
- cowsay/cowsay my bash script.
Download and extract cowsay.zip.
Extract the archive file with this command: unzip -d ./ cowsay.zip
If you already have a working cowsay and fortune-mod you can skip to step 4.
If you want to include extra cow files start at step 3.
Installation
1. Copy the perl script cowsay file in cowsay/bin/ to your bin directory path:
sudo cp cowsay/bin/cowsay /<path to bin>/. example: sudo cp cowsay/bin/cowsay /usr/bin/.
Make the perl file executable: sudo +x /usr/bin/cowsay
2. Create a cowthink symbolic link from cowsay in the same directory from step 1:
sudo ln -s /<path>/cowsay /<path>/cowthink example: sudo ln -s /usr/bin/cowsay /usr/bin/cowthink
3. Copy cow files directory (cowsay/cows) to your local cows directory:
sudo cp -r cowsay/cows /<path to the cows directory>/. example: sudo cp -r cowsay/cows /usr/share/cowsay/.
Discover the local cow files directory path with this command:
cowsay -l
Cow files in /usr/share/cowsay/cows:
apt bearface beavis.zen bill-the-cat bishop blowfish bong bud-frogs bunny
calvin cheese cock cower daemon default dolphin dragon dragon-and-cow duck
elephant elephant-in-snake elephant2 eyes flaming-sheep fox ghostbusters
gnu head-in hedgehog hellokitty kangaroo kilroy king kiss kitten kitty
knight koala kosh llama luke-koala mech-and-cow meow milk moofasa moose
mutilated pawn pony pony-smaller queen ren rook satanic sheep skeleton
small snort snowman sodomized stegosaurus stimpy supermilker surgery suse
telebears three-eyes tortoise turkey turtle tux udder unipony
unipony-smaller vader vader-koala www
4. Make sure that all your cow files in the library are of type <cow name>.cow
example: ls /usr/share/cowsay/cows
If some cow files are <cow name>.pm, remove these:
example: sudo rm /usr/share/cowsay/cows/*.pm
5. Ensure that your cow files can be read from the library:
example: sudo chmod 644 /usr/share/cowsay/cows/*
6. Copy the cowsay bash script file to a convenient location (place it into your documents or scripts directory).
Copy the bash cowsay script to a convenient directory: cp cowsay </path to cowsay>/.
Make the bash file executable: sudo chmod +x /<path to cowsay>/cowsay
Test to see if it works: <path to cowsay>/cowsay
7. Edit your .bashrc file to add an alias and then run the cowsay alias command:
alias cowsay='~/<path to cowsay>/cowsay'
(save and exit .bashrc)
clear && source ~/.bashrc
Now when you launch your terminal you will be greeted by a fortune telling cow (or some other animal).
A fortune thinking Chameleon!
How the Cowsay Program Works
Cowsay Program flowchart
Cowsay program flow chart.