Generate Random Quotes with a Shell Script [Part 2/3: From Quotationspage.com Online]

This post expands upon “Generate Random Quotes with Shell Scripts [Part 1/3: Your Own Specified Quotes]” where we took a look at how to generate a user defined list of quotes and then randomly select and display one of them (user_defined). Here in part 2, we’ll take a look at how to expand on this script to add two methods of random quote generation, both of which use the service provided by http://www.quotationspage.com (which, by the way, works in both Mac OSX and Linux). One of the methods we’ll call online_1mqotd which will obtain a quote from this javascript source, and the other we’ll call online_rand which will search randomly through the thousands of quote pages at quotationspage.com. I’ll let the script do the talking and explaining, so without further ado, here it is (usage instructions follow):


if [ $# -lt 1 -o $1 == "help" ]
	echo "Usage: $0 <user_defined|online_1mqotd|online_rand>"

## Grab a quote from a user defined list
if [ $source == "user_defined" ]

rand=$[ ( $RANDOM % $num_quotes ) + 1 ]

case $rand in
	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";;	

	echo "Random Quote: $quote" | fmt -80

## Grab and parse the "motivational quote of the day" from quotationspage.com.
# TODO: Parse the author as well.  Maybe use same method as online_rand.
elif [ $source == "online_1mqotd" ]
	quote=$(wget -q -O - http://www.quotationspage.com/data/1mqotd.js | grep "tqpQuote" |
	        sed "s/document.writeln('<dt class=\\'tqpQuote\\'>/"/g; s/</dt>');/"/g")

	echo "Random Quote: $quote" | fmt -80

## Grab and parse a random quote from quotationspage.com.  
# TODO: Only print author mini-bio if it exists, as opposed to all the time.
elif [ $source == "online_rand" ]
		rand_online=$[ ( $RANDOM % $num_online_quotes ) + 1 ]
		# In HTML source, <dt> is unique to quote and </dd> is unique to author
		# The field separators (FS) are either < or > which is [<>] in posix
		# TODO: Wanted to print as "quote -- author n (mini-bio)", but MacOSX 'fmt' is blah 
		quote=$(wget -q -O - "http://www.quotationspage.com/quote/$rand_online.html" |
		 grep -e "<dt>" -e "</dd>" | awk -F'[<>]' '{ 
			if($2 ~ /dt/)
			{ print $3 } 
			else if($4 ~ /b/)
			{ print "-- " $7 "  n(" $19 ")"} 
		# 5 Attempts at obtaining a quote.  Silently fail.
		while [ $i -lt 5 ]
			echo "$quote" | grep ERROR > /dev/null
			if [ $? -eq 0 ]
				i=`expr $i + 1`
				echo "Random Quote: $quote" | fmt -80

You can see the script in action here:

$ ./random_quote 
Usage: ./random_quote <user_defined|online_1mqotd|online_rand>

$ ./random_quote user_defined
Random 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.

$ ./random_quote online_1mqotd
Random Quote: "Patience serves as a protection against wrongs as clothes do
against cold. For if you put on more clothes as the cold increases, it will have
no power to hurt you. So in like manner you must grow in patience when you meet
with great wrongs, and they will then be powerless to vex your mind."

$ ./random_quote online_rand
Random Quote: Between two evils, I always pick the one I never tried before.  --
Mae West (US movie actress  (1892 - 1980))

In part 3 of 3 coming up Monday, we’ll conclude by taking a look at a clever way of utilizing this script in the Evolution email client. This way, your email recipients will always be pleasantly surprised by a random quote in your signature every time you respond.

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

  • [email protected]
    April 11th, 2010 at 10:40 am

    Hey All,

    I got an email from Jim with some feedback on this post and wanted to share:

    Hi Derek,

    Of course you could just port the fortune program along with any number of the readily available databases, or used with your own DB 🙂

    If you have perl, then this one line perl invocation will select any
    one line of the quotesfile with equal probability…

    perl -we’rand($.) < 1 && ($pick = $_) while ; print $pick’ quotesfile

    If the file contained one quote per line, it would generate a random quote.

    p.s. the one-liner came from Ben Okopnik of the Linuz Gazette, who got it from Randal Schwartz. Working out how it works takes some doing, and I’m not sure I remember the logic now. I just filed it away for a rainy day.

    My response for those of you interested:


    Once again, thanks for the feedback!

    I knew about the ‘fortune’ program, but wanted to get quotes from the larger online database of quotationspage.com (I have an iGoogle gadget that features quotes from there and I enjoy the variety). But, true… for most folks, I imagine ‘fortune’ would be just as good. ; )

    I haven’t gotten into perl before. Looks like it would be worthwhile to check out. Thanks.


  • Randal L. Schwartz
    April 14th, 2010 at 6:12 am

    I can’t claim to be the source of that one-pass random picker either. I got it from someone else in a Perl newsgroup or mailing list, and once I understood it, I’ve been using it where I can.

Share your thoughts, leave a comment!