diff options
author | Peter Palfrader <peter@palfrader.org> | 2018-01-20 19:37:07 +0100 |
---|---|---|
committer | Peter Palfrader <peter@palfrader.org> | 2018-01-20 19:37:07 +0100 |
commit | fdddcc5e95d227762849c741e7a5cdbdbe01669a (patch) | |
tree | 8edf090e41310753227188299764ded599581bb3 | |
parent | dd17ab947c9f32c749f45fa483bce9d1edcf6a60 (diff) |
dicewords: support dictionary
-rwxr-xr-x | dicewords | 39 |
1 files changed, 34 insertions, 5 deletions
@@ -1,11 +1,40 @@ -#!/bin/sh +#!/bin/bash + +set -e +set -u + +usage() { + echo "Usage: $0 [-d <dictionary] [count]" +} + +dictionary=american-english + +while getopts "hd:" OPTION; do + case "$OPTION" in + h) + usage + exit 0 + ;; + d) + dictionary="$OPTARG" + ;; + *) + usage >&2 + exit 1 + esac +done +shift $(($OPTIND - 1)) count=${1:-6} -if [ "$count" = "-h" ]; then - echo "Usage: $0 [count]" +if ! [[ $dictionary == *"/"* ]]; then + dictionary=/usr/share/dict/"$dictionary" fi -egrep '^[a-z]{1,6}$' /usr/share/dict/american-english | shuf | head -n "$count" | tr '\n' '-' | sed -e 's/-$//' -echo +if ! [ -e "$dictionary" ]; then + echo >&2 "$dictionary does not exist" + exit 1 +fi +egrep '^[a-z]{1,6}$' "$dictionary" | shuf | head -n "$count" | tr '\n' '-' | sed -e 's/-$//' +echo |