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