Piping fun in shell with fortune and cowsay

Somebody mentioned seashells.io to me. A nifty little service that let’s you pipe output from your shell’s command line directly to the web.

What better use for this than to pipe random funny quotes?

TL;DR

If you want to check out the output – just go here https://seashells.io/v/FCPsK8Sh. It’s running from my shell – so may die any moment.

Show me how!

First, you need to install a couple of packages.

  • cowsay prints an ascii image to the screen, that looks like a cow ( or one of the specified animals).
  • fortune prints a random quote or a “fortune-cookie” type message from a collection of quotes
  • fortune-min is needed on some systems (like Ubuntu), as fortune only comes with the engine, not the db of quotes. Not needed on mac.
  • seashell is a client for seashells.io website. You don’t have to install it, you can just use netstat. It’s up to you. But you do need python & pip available.
sudo apt-get install -y cowsay fortune fortunes-min

# If you are on macOSX, brew is your friend
brew install cowsay fortune 

# if you want seashells client instead of nc
pip install seashells

Now that we have prerequisites, let’s actually use the magic of pipe to combine all the tool togeather!

Note: The choice of animals available to cowsay varies by distribution . For instance, macOSx has blowfish but ubunutu‘s package, doesn’t

If you use fish shell

# Let's wrap some functionality into a handy function
function random_quote
  set animal (random choice {default,dragon,dragon-and-cow,elephant,moose,stegosaurus,tux,vader})
  fortune -s | cowsay -f $animal
end

# a trick to pipe from loop to another process. Fish only.  
fish -c 'while true; sleep 15; random_quote; end' | nc seashells.io 1337

If you use bash shell

random_quote() {
  choices=("dragon" "dragon-and-cow" "elephant" "moose" "stegosaurus" "tux" "vader")
  animal=${choices[$RANDOM % ${#choices[@]} ]}
  fortune -s | cowsay -f $animal
}

while true; do sleep 15; random_quote; done | nc seashells.io 1337

Also published on Medium.

Published by

Nick Gorbikoff

Nick Gorbikoff is a Ruby & React DevOps engineer and hands-on CTO working on ERP Systems. In his spare time, Nick is a fearless follower of 2 kids and an awesome wife, collector of useless historical trivia, tinkerer of sorts, fast food connoisseur and aspiring martial artist.