#!/bin/ksh ################################################################################ #DOCBEGIN # # Reads a text quotes file and grabs a random line to print out to stdout (screen). # # You can select a pattern to look for in the quotes file. # You can select a specific quote file (with location). # # Created by Matt Baker on 3/2/91 # Modfied by Matt Baker on 6/11/2008 # #DOCEND ################################################################################ DEBUG=0 QUOTE_FILE=$HOME/etc/quotes QUOTE_FILE_SIZE=$HOME/etc/quotes.size ########### # PARSING: read in options & arguments ########### while (( $# )) do case $1 in -f) shift if (( $# )) then QUOTE_FILE=$1 shift if [[ ! -f $QUOTE_FILE ]] then print "ERROR:5: quote file $QUOTE_FILE cannot create" exit 5 fi else print "ERROR:1: quote file (-f) needs an argument" exit 1 fi ;; -debug) DEBUG=1 shift ;; -info | -doc) awk '/DOCBEGIN/, /DOCEND/' $0 \ | sed -e 's/^#//' \ | sed -e '/DOCBEGIN/d' \ | sed -e '/DOCEND/d' \ | more exit 0 ;; -h*) if [[ $1 != -h ]] then print "\n OPTION: $1 is invalid.\n" else print "" fi ############################################################################## cat << EOF Usage: $0: Usage: $0: [-debug] [-f inputFILE] [pattern] Usage: $0: -info | -doc Usage: $0: -h no args: defaults to just a random quote pattern: search for pattern and then print a random line with that pattern. -debug: debug mode, does a 'set -x' -info|doc: print out header of program (document) -h: help - command synopsis (this listing) EOF ############################################################################## exit 1 ;; *) PATTERN="$*" shift ;; esac done if (( DEBUG )) then set -x fi if [[ ! -f $QUOTE_FILE_SIZE || $QUOTE_FILE -nt $QUOTE_FILE_SIZE ]] then SIZE=$(grep -v \# $QUOTE_FILE \ | wc -l \ | awk '{print $1}') echo "SIZE=$SIZE" > $QUOTE_FILE_SIZE else SIZE=$(awk -F= '/SIZE/ {print $2}' $QUOTE_FILE_SIZE) fi #get all non-commented lines, change tabs to CR and fold if [[ -n $PATTERN ]] then SIZE=$(grep -v \# $QUOTE_FILE \ | grep -i "$PATTERN" \ | wc -l \ | awk '{print $1}') if (( $SIZE )) then let RAND=$RANDOM%$SIZE let RAND=$RAND+1 grep -v \# $QUOTE_FILE \ | grep -i "$PATTERN" \ | sed -n "$RAND"p \ | tr '\011' '\012' #| tr '\011' '\012' \ #| fold -w 79 else print "ERROR:3:Pattern \"$PATTERN\" not found." exit 3 fi else let RAND=$RANDOM%$SIZE let RAND=$RAND+1 grep -v \# $QUOTE_FILE \ | sed -n "$RAND"p \ | tr '\011' '\012' #| tr '\011' '\012' \ #| fold -w 79 fi