#!/bin/bash # 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. setglobal awkscript = ""/tmp/randomquote.awk.$Pid"" if test $Argc -ne 1 { echo "Usage: randomquote datafilename" > !2 exit 1 } elif test ! -r $1 { echo "Error: quote file $1 is missing or not readable" > !2 exit 1 } trap "$[which rm] -f $awkscript" 0 cat << ''' > $awkscript BEGIN { srand() } { s[NR] = $0 } END { print s[randint(NR)] } function randint(n) { return int (n * rand() ) + 1 } ''' > $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