DEV Community

Morgan Murrah
Morgan Murrah

Posted on

Fun unix basics with fortune and cowsay

$ cowsay "lets have fun on the command line"
 ___________________________________ 
< lets have fun on the command line >
 ----------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Enter fullscreen mode Exit fullscreen mode

The following exercise is intended to provide some simple enjoyment and cover some some unix command line concepts. This tutorial assumes you:

  • are able to use a terminal i.e opening the terminal application on Mac OS and have some basic unix knowledge

  • have homebrew installed or are otherwise able to install the two programs fortune and cowsay if they are not on your system

    • brew install cowsay
    • brew install fortune

There are alternate ways to install these programs on a variety of systems and in different languages.

  • Open a terminal

Say Hello the old way echo hello.

$ echo hello
hello
Enter fullscreen mode Exit fullscreen mode

Now try saying it with cowsay cowsay hello:

$ cowsay hello
 _______ 
< hello >
 ------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
Enter fullscreen mode Exit fullscreen mode

:).

Get a pseudo random fortune epigram from the program fortune:

$ fortune
"The History of every major Galactic Civilization tends to pass through
three distinct and recognizable phases, those of Survival, Inquiry and
Sophistication, otherwise known as the How, Why and Where phases.
"For instance, the first phase is characterized by the question 'How can
we eat?' the second by the question 'Why do we eat?' and the third by
the question 'Where shall we have lunch?'"
-- Hitchhiker's Guide to the Galaxy

Enter fullscreen mode Exit fullscreen mode

Get your Cow to say a fortune! (by using the pipe | operator)

$ fortune | cowsay
 ___________________________________ 
/  "You boys lookin' for trouble?"  \
|                                   |
| "Sure. Whaddya got?"              |
|                                   |
\ -- Marlon Brando, "The Wild Ones" /
 ----------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
Enter fullscreen mode Exit fullscreen mode

We are really moo'ving right along!

In this simple progression we have seen how two simple programs can interact with each other, the standard output of fortune can be redirected into cowsay which creates the ASCII cow with the enclosed text!

Lets take this piping arrangement to the next level with the program tee that takes stdin and then by default overwrites a file or can append to a file, while also passing that stdin to stdout to the console.

$ fortune | cowsay | tee save-cow-to-file.txt 

 ________________________________________ 
/ When you dial a wrong number you never \
\ get a busy signal.                     /
 ---------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


$ cat save-cow-to-file.txt 
 ________________________________________ 
/ When you dial a wrong number you never \
\ get a busy signal.                     /
 ---------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
Enter fullscreen mode Exit fullscreen mode

Now we have a copy of the cow's fortune saved to a file. Lets append to that file with the -a flag on tee:

$ fortune | cowsay | tee -a  save-cow-to-file.txt 
 ______________________________________ 
/ Competence, like truth, beauty, and  \
| contact lenses, is in the eye of the |
| beholder.                            |
|                                      |
\ -- Dr. Laurence J. Peter             /
 -------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


$ cat save-cow-to-file.txt 
 ________________________________________ 
/ When you dial a wrong number you never \
\ get a busy signal.                     /
 ---------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
 ______________________________________ 
/ Competence, like truth, beauty, and  \
| contact lenses, is in the eye of the |
| beholder.                            |
|                                      |
\ -- Dr. Laurence J. Peter             /
 -------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Enter fullscreen mode Exit fullscreen mode

You now have a pipeline going able to create a collection of generated cows with interesting epigrams written to a file!

There is plenty of room for creative expression- check out all the flags on cowsay by checking its man page man cowsay.

Top comments (0)