#!/usr/bin/sh -e # Fortune implementation in awk usage() { echo "fortune -- a simple awk/POSIX sh implementation of fortune" echo "" echo "\t-h --help show this help message" echo "\t--file=\$FILE specify path to cookie file" } FILE="$(find /usr/share/fortune/ -type f -not -name '*.dat')" while [ "$1" != "" ]; do PARAM=`echo $1 | awk -F= '{print $1}'` VALUE=`echo $1 | awk -F= '{print $2}'` case $PARAM in -h | --help) usage exit ;; -f | --file) FILE=$VALUE ;; *) echo "ERROR: unknown parameter \"$PARAM\"" usage exit 1 ;; esac shift done awk '$1 == "%" { sub(/%/, "") ++f } { fortunes[f] = fortunes[f] (length(fortunes[f])?"\n":"") $0 } END { "date +%N" | getline time srand(time) print fortunes[int(rand() * f) + 1] }' $FILE