Monday, August 18, 2008

Unix and Linux Online Dictionary Shell Script

Hey There,

Today's Linux/Unix bash shell script is is a blatant exploitation of a post we did, not too long ago, where we put together a Thesaurus shell script. This time we're creating a shell script to emulate a full-function dictionary. The actual online source we draw from (Dictionary.com) deserves the credit for knowing what virtually every word in the English language means ;)

A dictionary can, obviously, come in handy when you're reading a book. As to its application on Unix or Linux, the usefulness is debatable. I do find, from time to time, that I don't know what a word in a man page, or info file, means (although that word is usually not in the dictionary, anyway ;). At times like those, this script has the potential to come in pretty handy. I'm just lazy enough to script out direct access to an online dictionary rather than open my web browser ;)

Like our previous script, this script uses wget and sed. This script was also written using "brute force" scripting (that's my term for writing it, directly, as I think it). I'm sure it could be made more comprehensive and technically efficient, but it's such a short script, anyway, that I decided not to kill myself rethinking it (or removing duplicate "sed" statements ;). The output could use some massaging, too, but at least I've got it to the point where it's legible.

Below is a screen shot of the script's output. Your results may vary. I didn't really have the time to test every possible way this could go wrong (Thank God ;)

Click the picture below to see it in full size:

Dictionary Script Output

Below is a screen shot of the script's output when choosing a word with multiple returns:

Click the picture below to see it in full size:

Long Results

As a note of caution, be prepared for multiple screens worth of output. If you need to look up the meaning of a word like "work," you should probably pipe the command to "more", like:

host # ./dict.sh word|more

Hope you enjoy this script, and can find some use for it. Any suggestions for improvement would be greatly appreciated. You probably noticed, from the above screenshots, that, every once in a while, a definition will get skipped... so that's still on the to-do list ;)

Now, to tackle the online Encyclopedia :)

Cheers,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/bash

# dict.sh - Know what you mean!
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

if [ $# -lt 1 ]
then
echo "Usage: $0 YourDictionaryTerm"
echo "May be two or more words separated by spaces"
echo "but only one definition per execution."
echo "Ex: $0 phone"
echo "Ex: $0 phone number"
exit 1
fi

args="$@"
wget=/usr/bin/wget

if [ $# -gt 1 ]
then
args=`echo $args|sed 's/ /%20/g'`
fi

echo

$wget -nv -O - http://dictionary.reference.com/browse/"$args" 2>&1|egrep -v 'Share This|Amazon.com|Reference.com|Thesaurus.com|Jobs|Copyright|To learn more|Download Now|Buy the Book|Origin'|egrep -i 'adjective|noun|verb|luna-Ent:|No results found|go/| suggestions:'|sed -e 's/$/\n/' -e :a -e 's/<[^>]*>/ /g;/</N;//ba' -e 's/\&[A-Za-z]*\;/ /g' -e 's/ / /g' -e 's/\(.\)$/\1\n/' -e 's/Would you.*$//' -e 's/[^A-Za-z0-9\. \n]//g' -e 's/\([0-9]\.\)/\n\1/g' -e 's/^.*Pronunciation//g'

exit 0


, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.