Dictionary Shell Script

Once in a while everyone needs to use some type of reference material be it a dictionary, a book, Internet or a friend, when writing whatever it is you write. Of course, your mileage will vary but for me, I frequently use the Internet when writing this blog. For instance, I found myself going to http://dictionary.com or plugging a word in Google to find out the correct spelling and or meaning of a word. Since I favor the power of the Linux command line over most GUI programs any day, I thought to myself, “Can this be scripted?” Why yes it can! I love the flexibility of the Linux shell and its scripting abilities. This is one repetitive task that became a little too tedious for me so scripting it was the way to go.

What did I do? Well, I created my own shell script that when ran with an argument, goes out to “Internet land” (actually http://dictionary.reference.com) and retrieves any relative information matching your given argument.

It has a couple built in checks to make sure it needs everything to actually retrieve the information you have asked. First it checks to make sure that you gave it an argument (duh) and second, it makes sure you have the Linux program lynx installed on your system. Most Linux systems come standard with this program today so you might not need to worry. Once all conditions have been met, it runs lynx and retrieves any relative information.

Anyway, check it out. Its just an ordinary bash shell script so just:

  • copy and paste it to a file on your Linux box
  • chmod 755 file ( I named mine getdef.sh )
  • and run it!

Here is my dictionary shell script:


#!/bin/bash
###################################################
#
# Name: getdef.sh
# Author: jtnez
# Date: 10/04/05
#
###################################################

 
# path to lynx on your system
_LYNX=/usr/local/bin/lynx

###################################################
#
# DO NOT EDIT BELOW UNLESS YOU KNOW WHAT YOU ARE DOING!
#
###################################################

# URL used by dictionary.reference.com
_URL=http://dictionary.reference.com/search?q=

# a shortcut
_WORD=$1

# the actual fuzzy logic of the script starts here...

if [ -z "$1" -o $# -gt "1" ]; then
 echo -e "\nUSAGE: $(basename $0) [word]\n"
 exit

elif [ ! -f $_LYNX ]; then
  echo -e "\nSorry, lynx was not found ob this system\n"
  exit

else
  $_LYNX -cfg=/dev/null -dump "${_URL}${_WORD}"

fi

Piping it to more or less helps readability… Have fun and enjoy!


About this entry