Skip to content

Rainbow Cow Fortune Teller

William Smyth edited this page May 24, 2020 · 1 revision

Rainbow-Cow Fortune Teller

A fun script to be placed in .bashrc || .profile which will tell you a fortune from a cow in rainbow technicolors.


Installation and Configuration

Install on Ubuntu/Debian or other Linux distros, MacOS, or Windows WSL distros

sudo apt install fortune cowsay lolcat -y

This script, once inserted in your .bashrc or .profile file in your home directory, will search for a custom BASH function that I've written, and which can be found farther down on this page. If it doesn't find the BASH function, it will search for a script variation ** (make sure to change this to a location that exists on your machine) ** , if the shell script isn't located, it will perform the scripted process itself. The order of checks and operations is:

  1. BASH function
  2. BASH/shell script
  3. .bashrc script

NOTE: You can modify this however you like. I was just trying to make it failsafe on my own machine, so no matter what, it would run. The only thing I didn't do was have it install the applications itself, but that could easily be done, if desired.

BASH Shell Function


Need to have this function file sourced with .bashrc or .profile using:

# add this to your bash .profile or .bashrc file and change filepath to match your location and filename
if [ -f "~/code/scripts/bash/my_bash_functions" ]; then
    . "~/code/scripts/bash/my_bash_fucntion"
fi

the function that needs to be saved in a file and referenced by your bash .profile or .bashrc

# fortune told by rainbow cow
function rainbow_cow_fortune(){
  # variables for app lookup
  GAMES=/usr/games
  set -- "fortune" "cowsay" "lolcat"
  
  if [ -d "$GAMES" ]; then
    # declare some variables
    CHECK="true"
    declare -a MISSING

    # loop through app list
    for g in $@; do
      if [ -f "$GAMES/$g" ] && [ "$CHECK" == "true" ]; then
        CHECK="true"
      else
        CHECK="false"
        MISSING+=("$GAMES/$g")
      fi
    done

    # execute if CHECK is good, otherwise report missing app
    if [ $CHECK == "true" ]; then
      fortune | cowsay | lolcat
    else
      echo "One or more apps aren't detected on your system!"
      echo "Apps searched for: $@"
      echo "Missing app: ${MISSING[0]}"
    fi
  fi
}

My .bashrc Script Entry


add this to your .bashrc or .profile and change the directory paths accordingly.

if [ -n "$(LC_ALL=C type -t rainbow_cow_fortune)" ] && [ "$(LC_ALL=C type -t rainbow_cow_fortune)" == function ]; then
  echo 'function'
  rainbow_cow_fortune
elif [ -x ~/code/scripts/bash/rainbow-cow-fortune.sh ]; then
  echo 'script:'
  ~/code/scripts/bash/rainbow-cow-fortune.sh
else
  echo 'bashrc:'
  GAMES=/usr/games
  set -- "fortune" "cowsay" "lolcat"
  if [ -d "$GAMES" ]; then
    # declare some variables
    CHECK="true"
    declare -a MISSING=
  
    # loop through app list
    for g in $@; do
      if [ -f "$GAMES/$g" ] && [ "$CHECK" == "true" ]; then
	CHECK="true"
      else
        CHECK="false"
        MISSING+=("$GAMES/$g")
      fi
    done
  
    # execute if CHECK is good, otherwise report missing app
    if [ $CHECK == "true" ]; then
      fortune | cowsay | lolcat
    else
      echo "One or more apps aren't detected on your system!"
      echo "Apps searched for: $@"
      echo "Missing app: ${MISSING[0]}"
    fi
  fi
fi

Other Works, Some Thoughts, and a Disclaimer

Other Fun and Useful Stuff


Checkout my GitHub repo that has all of these and more BASH goodies.

Thoughts


If you'd like to make these better in some way, please feel free to create an issue on my repo, or fork my repo; update/mod the code, and submit a Pull Request on github.com/devopsmyth/bash-mash.

Disclaimer


Do NOT forget to USE-AT-YOUR-OWN-RISK with any scripts you find here or anywhere else online. I don't claim to have written any of the scripts on my repo entirely from scratch, I'm sure I borrowed ideas and snippets from lots of places. If you see something that resembles some other work, there's a possibility that I learned from that same resource. I am by no means an expert in Linux, or BASH, I'm just a hobbyist and IT professional with many years of experience. This is my first foray into sharing my personal stuff online, so keep the criticisms useful, please.