X-Git-Url: https://git.armaanb.net/?a=blobdiff_plain;f=fortune;h=afb967c5833679af35705ec5e21b64a63731ff39;hb=26ce4f633da6e65105ff9d9046b760d28a4a3809;hp=a8589a5f9e6a5ba8c4af5d12501becaf16c7270e;hpb=48374cb189a58ab6a44871c7c744a1f67f986089;p=bin.git diff --git a/fortune b/fortune index a8589a5..afb967c 100755 --- a/fortune +++ b/fortune @@ -2,22 +2,28 @@ # Fortune implementation in POSIX sh/awk usage() { - echo 'Usage: fortune [OPTIONS] + echo 'Usage: fortune [OPTIONS] a simple POSIX sh/awk implementation of fortune - -h, --help show this help message - -l, --list list installed cookie files - -c FILE, --cookie FILE specify cookie file - -n LENGTH, --min LENGTH specify min fortune length - -N LENGTH, --max LENGTH specify max fortune length + -h, --help show this help message + -l, --list list installed cookie files + -c FILE, --cookie FILE specify cookie file + -n LENGTH, --min LENGTH specify min fortune length + -N LENGTH, --max LENGTH specify max fortune length - fortune looks for fortunes in $FORTUNEDIR, which defaults to - /usr/share/fortune. + fortune looks for fortunes in $FORTUNEDIR, which defaults to + /usr/share/fortune. Exit codes: - 0 success - 1 argument error - 2 parse error' + 0 success + 1 generic error + 2 argument parsing error' +} + +parse_err() { + echo "$1" + $2 + exit 2 } DIR=${FORTUNEDIR:-"/usr/share/fortune/"} @@ -26,64 +32,64 @@ MAXLENGTH=1000000000000 MINLENGTH=0 while [ "$1" != "" ]; do - case $1 in - -h | --help) - usage - exit - ;; - -c | --cookie) - FILE=$DIR/$2 - ;; - -n | --min) - MINLENGTH=$2 - ;; - -N | --max) - MAXLENGTH=$2 - ;; - -l | --list) - ls -1 $DIR - exit - ;; - *) - echo "ERROR: unknown option \"$1\"" - usage - exit 1 - ;; - esac - shift 2 + case $1 in + -h | --help) + usage + exit + ;; + -c | --cookie) + FILE=$DIR/$2 + ;; + -n | --min) + MINLENGTH=$2 + ;; + -N | --max) + MAXLENGTH=$2 + ;; + -l | --list) + ls -1 $DIR + exit + ;; + *) + parse_err "ERROR: unknown option \"$1\"" usage + ;; + esac + shift 2 done [ "$MAXLENGTH" -lt "$MINLENGTH" ] && { - echo "ERROR: maximum length is less than minimum length" - exit 2 + parse_err "ERROR: maximum length is less than minimum length" } [ -d "$FILE" ] && { - echo "ERROR: directory given, please provide a file or glob of files" - exit 2 + parse_err "ERROR: directory given, please provide a file or glob of files" +} + +[ "$(find $FILE -maxdepth 1 -print -quit 2> /dev/null)" ] || { + parse_err "ERROR: no such file or directory" } awk -v maxlen=$MAXLENGTH -v minlen=$MINLENGTH \ -'BEGIN { - f++ + 'BEGIN { + f++ } $1 == "%" { - len = length(fortunes[f]) - if ( len>maxlen || lenmaxlen || len