76 Displaying Random Text


#76 Displaying Random Text

The built-in server-side include features offer some wonderful ways to expand and extend your website. One way that's a favorite with many webmasters is the ability to have an element of a web page change each time the page is loaded. The ever-changing element might be a graphic, a news snippet, or a featured subpage, or it might just be a tag line for the site itself, but one that's slightly different for each visit to keep the reader interested and ” hopefully ” coming back for more.

What's remarkable is that this trick is quite easy to accomplish with a shell script containing an awk program only a few lines long, invoked from within a web page via an SSI include (see Script #75 for an example of SSI directive syntax and naming conventions for the file that calls the server-side include). Let's have a look.

The Code

 #!/bin/sh # randomquote - Given a one-line-per-entry datafile, this #   script randomly picks one line and displays it. Best used #   as an SSI call within a web page. awkscript="/tmp/randomquote.awk.$$" if [ $# -ne 1 ] ; then   echo "Usage: randomquote datafilename" >&2   exit 1 elif [ ! -r "" ] ; then   echo "Error: quote file  is missing or not readable" >&2   exit 1 fi trap "/bin/rm -f $awkscript" 0 cat << "EOF" > $awkscript BEGIN { srand() }       { s[NR] = 
 #!/bin/sh # randomquote - Given a one-line-per-entry datafile, this # script randomly picks one line and displays it. Best used # as an SSI call within a web page. awkscript="/tmp/randomquote.awk.$$" if [ $# -ne 1 ] ; then echo "Usage: randomquote datafilename" >&2 exit 1 elif [ ! -r "$1" ] ; then echo "Error: quote file $1 is missing or not readable" >&2 exit 1 fi trap "/bin/rm -f $awkscript" 0 cat << "EOF" > $awkscript BEGIN { srand () } { s[NR] = $0 } END { print s[randint(NR)] } function randint(n) { return int (n * rand()) + 1 } EOF awk -f $awkscript < "$1" exit 0 
} END { print s[randint(NR)] } function randint(n) { return int (n * rand()) + 1 } EOF awk -f $awkscript < "" exit 0

How It Works

This script is one of the simplest in the book. Given the name of a data file, it checks to ensure that the file exists and is readable, and then it feeds the entire file to a short awk script, which stores each line in an array (a simple data structure), counting lines, and then randomly picks one of the lines in the array and prints it to the screen.

Running the Script

The script can be incorporated into an SSI-compliant web page with the line

 <!--#exec cmd="randomquote.sh samplequotes.txt"--> 

Most servers require that the web page that contains this SSI include have an .shtml filename suffix, rather than the more traditional .html or .htm . With that simple change, the output of the randomquote command is incorporated into the content of the web page.

The Results

The last few lines of Figure 8-7, in Script #75, show a randomly generated quote as part of a web page. However, given a data file of one-liners borrowed from the Trivial.net tag line file (see http://www.trivial.net/ ), this script can also be tested on the command line by calling it directly:

 $  randomquote samplequotes.txt  Neither rain nor sleet nor dark of night... it's Trivial.net $  randomquote samplequotes.txt  Spam? Not on your life. It's your daily dose of Trivial.net 

Hacking the Script

It would be remarkably simple to have the data file that randomquote uses contain a list of graphic image names , for example, and then use this simple script to rotate through a set of graphics. There's really quite a bit more you can do with this idea once you think about it!




Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net