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
23 DIR=${FORTUNEDIR:-"/usr/share/fortune/"}
24 FILE="$(find $DIR -type f -not -name '*.dat')"
25 MAXLENGTH=1000000000000
28 while [ "$1" != "" ]; do
48 echo "ERROR: unknown option \"$1\""
56 [ "$MAXLENGTH" -lt "$MINLENGTH" ] && {
57 echo "ERROR: maximum length is less than minimum length"
62 echo "ERROR: directory given, please provide a file or glob of files"
66 awk -v maxlen=$MAXLENGTH -v minlen=$MINLENGTH \
72 len = length(fortunes[f])
73 if ( len>maxlen || len<minlen ) {
82 fortunes[f] = fortunes[f] (length(fortunes[f])?"\n":"") $0
86 "date +%N" | getline time
88 print fortunes[int(rand() * (f - 2)) + 1]