Skip to content

Instantly share code, notes, and snippets.

@janmoesen
Created October 14, 2011 11:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janmoesen/1286835 to your computer and use it in GitHub Desktop.
Save janmoesen/1286835 to your computer and use it in GitHub Desktop.
Word of the day in Bash
# Read a random word from the dictionary and try to get its definition from
# Merriam-Webster.
function word-of-the-day {
local file="${1:-/usr/share/dict/words}";
if ! [ -r "$file" ]; then
echo "Cannot read from dictionary file: $file" 1>&2;
return 1;
fi;
local IFS=$'\n' words=($(<"$file"));
local num_words=${#words[@]};
# The silly math is to half-assedly compensate for $RANDOM's range being
# from 0 to 32767, while there are many more words in the dictionary.
local word="${words[$((($num_words / 32767 * $RANDOM) % $num_words))]}"
echo "$word";
type -t lynx > /dev/null && \
lynx -width 1024 -nolist -dump "http://www.merriam-webster.com/wdictionary/$word" | \
awk '/^ *([a-z] )?:/ { sub(/[^:]*: /, "* "); print $0; }';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment