2 # Fortune implementation in POSIX sh/awk
5 echo 'Usage: fortune [OPTIONS]
6 a simple POSIX sh/awk implementation of fortune
8 -h, --help show this help message
9 -l, --list list installed cookie files
10 -c FILE, --cookie FILE specify cookie file
11 -n LENGTH, --min LENGTH specify min fortune length
12 -N LENGTH, --max LENGTH specify max fortune length
14 fortune looks for fortunes in $FORTUNEDIR, which defaults to
20 2 argument parsing error'
29 DIR=${FORTUNEDIR:-"/usr/share/fortune/"}
30 FILE="$(find $DIR -type f -not -name '*.dat')"
31 MAXLENGTH=1000000000000
34 while [ "$1" != "" ]; do
54 parse_err "ERROR: unknown option \"$1\"" usage
60 [ "$MAXLENGTH" -lt "$MINLENGTH" ] && {
61 parse_err "ERROR: maximum length is less than minimum length"
65 parse_err "ERROR: directory given, please provide a file or glob of files"
68 [ "$(find $FILE -maxdepth 1 -print -quit 2> /dev/null)" ] || {
69 parse_err "ERROR: no such file or directory"
72 awk -v maxlen=$MAXLENGTH -v minlen=$MINLENGTH \
78 len = length(fortunes[f])
79 if ( len>maxlen || len<minlen ) {
88 fortunes[f] = fortunes[f] (length(fortunes[f])?"\n":"") $0
92 "od -A n -t d -N 3 /dev/urandom" | getline seed
94 print fortunes[int(rand() * (f - 2)) + 1]