Generate Random Quotes with a Shell Script [Part 1/3: User Defined/Specified Quotes]

Here’s a method for generating randomized quotes in a shell script based on a list of quotes that has been populated by you. Most of what you’ll see is pretty self-explanatory and easy to follow. I’ve scattered comments into the source as well for even more verbosity.


# Keep this updated when you add or take away quotes on the case list

# Generate a random quote number variable, 'rand'
rand=$[ ( $RANDOM % $num_quotes ) + 1 ]
case $rand in  #BEGIN CASE
        1) quote="Some things Man was never meant to know. For everything else, there's Google.";;
        2) quote="The Linux philosophy is 'Laugh in the face of danger. Oops. Wrong One. 'Do it yourself'. Yes, that's it.  -- Linus Torvalds";;
        3) quote="... one of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -- Robert Firth";;
        4) quote="There are 10 kinds of people in the world, those that understand trinary, those that don't, and those that confuse it with binary.";;
        5) quote="My software never has bugs. It just develops random features.";;
        6) quote="The only problem with troubleshooting is that sometimes trouble shoots back.";;
        7) quote="If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.";;
        8) quote="You know you're a geek when... You try to shoo a fly away from the monitor with your cursor. That just happened to me. It was scary.";;
        9) quote="We all know Linux is great... it does infinite loops in 5 seconds. - Linus Torvalds about the superiority of Linux on the Amterdam Linux Symposium";;
        10) quote="By golly, I'm beginning to think Linux really *is* the best thing since sliced bread.  -- Vance Petree, Virginia Power";;
esac  # END CASE

# Display the random quote from case statement, and format it to line wrap at 80 characters
echo "Random Quote: $quote" | fmt -80

To run this script, simply create a new file, copy/paste the contents below, make the script executable, and then run it. Something like this ought to get you started:

vi random_quote
# Copy/paste the script contents to new file
chmod +x random_quote

This was part 1 of 3 in a series of generating random quotes in a shell script. Tomorrow, I’ll discuss part 2 of 3 which will be how to generate a random quote from The Quotations Page. The final part will be on example usage of this script where I will show you how to use it to insert random quotes as a signature in the Evolution email client. See you then.

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:

There's 2 Comments So Far

Who Linked To This Post?

  1. Generate Random Quotes with a Shell Script [Part 2/3: From Quotationspage.com Online] | The Linux Daily
  2. Generate Random Quotes with a Shell Script [Part 3/3: Integrate with Evolution Email] | The Linux Daily

Share your thoughts, leave a comment!