Work the Shell - Movie Trivia—Finally!
It's been one of those proverbial journeys of a thousand steps, but I think we're finally ready to start generating some movie trivia after spending the past few months doing all the underlying tool development. You'll recall that we're grabbing the top 250 movies list from Amazon's IMDb site, then getting the release year of each movie and storing it in a database.
Separately, we chewed on the interesting problem of coming up with adjacent years for a given year in time, recognizing that the older the movie, the more of a spread we want between years, because precious few people will guess incorrectly that a movie released in 2007 was released in 1983, but a movie released in 1947 could stymie people who might think it came out in 1931.
Now, it's time to put the pieces together.
The last column dug in to the year spread, ending with a script that produced a likely adjacent year for a given year. We need to refine this script, because what we want to produce are three different year possibilities, two that are likely but wrong and one that's the correct year, without duplicates.
First, let's make the code that generates a reasonable adjacent year a script function:
get_random()
{
delta="$(( $RANDOM % $factor + 1))"
add="$(( $RANDOM % 2 ))"
if [ $add -eq 1 ] ; then
closeyear="$(( $releasedate + $delta ))"
else
closeyear="$(( $releasedate - $delta ))"
fi
if [ $closeyear -gt $thisyear ] ; then
closeyear="$(( $releasedate - $delta ))"
fi
}
Next, given that we can't gracefully return a value short of using a global variable, here's how we can leverage the function:
get_random match1=$closeyear
That gets us the first year guess, easily enough. But, the next guess needs to be different from the first. How to do that? In a while loop:
match2=$match1 # needs an initial value while [ $match2 -eq $match1 ] ; do get_random match2=$closeyear done
This is slightly risky, because there is the possibility of an infinite loop if the code never finds a random year value that differs, but I'll ignore that for now.
Now we have three year values: two incorrect ones, $match1 and $match2, and the correct value, $releasedate. How to give them back to the calling routine sorted? Easy:
echo "$match1 $match2 $releasedate" | sort -n
And, that's the function. Give it a year, and it'll return three: two that are close but wrong, and one that's correct. For example:
$ ./year-delta.sh 1975 1981 1971 1975 $ ./year-delta.sh 1999 2000 1998 1999 $ ./year-delta.sh 1938 1948 1935 1938
That's exactly what we want. Now, how to integrate this into the bigger script that grabs a random line from the IMDb database and then presents it in a workable fashion?
Once you remember the trick of $(( $RANDOM % some-value)), it should be straightforward to get a random line from a data file:
lines="$(wc -l < $filmdb | sed 's/ //g')"
randline=$(( $RANDOM % $lines + 1 ))
match="$(sed -n "${randline}p" < $filmdb)"
As I've written about before, wc is one of your best friends in script writing, because it's easy. But, it's also frustrating that there's no way to turn off the superfluous white space it generates. That's why the first line includes a call to sed to axe any spaces that are added. Somewhere, in a parallel universe to our own, there's an -n flag to wc that says “no padding” and makes this forevermore unnecessary. Sadly, we aren't in that universe, so just about every time you use wc, you have to strip out the white space at the same time.
The result of these three lines is that match has a value similar to:
The Lord of the Rings: The Two Towers|2002
Now we need to split it into two fields, which is easily, if tediously, done:
title="$(echo $match | cut -d\| -f1)" relyear="$(echo $match | cut -d\| -f2)"
And, finally, it's time to invoke the random years function that will, if you recall, generate one correct and two incorrect years:
years=$($randomyears $relyear)
Finally, let's pull the three years into separate variables and then output an attractive trivia query:
year1="$(echo $years | cut -d\ -f1)" year2="$(echo $years | cut -d\ -f2)" year3="$(echo $years | cut -d\ -f3)" echo "IMDb Top 250 Movie #$randline: Was $title released in $year1, $year2 or $year3?"
Not too shabby! Let's see how it works:
$ ./generate-trivia-question.sh IMDb Top 250 Movie #82: Was "Some Like It Hot" released in 1950, 1959 or 1963? $ ./generate-trivia-question.sh IMDb Top 250 Movie #118: Was "Mononoke-hime" released in 1994, 1995 or 1997? $ ./generate-trivia-question.sh IMDb Top 250 Movie #250: Was "Planet of the Apes" released in 1967, 1968 or 1969?
Perfect, perfect!
That's about all we have space for in this column, but we've come a long, long way from the URL for a Web page that lists some top movies to a nice little trivia engine that's fast and fun!
Next month, we'll look at how to inject the trivia into the Twitterstream. Want to see it in action? By the time you read this column, it'll be live at twitter.com/FilmBuzz (along with movie commentary and much more).
Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at www.intuitive.com, and he also offers up tech support at AskDaveTaylor.com. Follow him on Twitter if you'd like: twitter.com/DaveTaylor.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Designing Electronics with Linux
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Using Salt Stack and Vagrant for Drupal Development
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




3 hours 8 min ago
3 hours 42 min ago
4 hours 40 min ago
5 hours 31 min ago
9 hours 32 min ago
13 hours 20 min ago
13 hours 28 min ago
15 hours 42 min ago
18 hours 12 min ago
1 day 4 hours ago