Google Site SearchFN Site Search FN Blog Login FN Blog Login
Site Navigation:
 
 

FUN WITH SHELLS: Define; a google-based web dictionary on the command line

by Andrew Newman (CREATED: 2004-03-05)

This is part of a series, called "Fun with Shells". The point of these articles will be to not only pass around some incredibly useful simple shells, but also, hopefully encourage people to jump in and either modify these or write their own.

This contribution is a wonderful little script to call up Google's web dictionary, strip off unnecessary information, reformat it a bit, and display definitions sized to your current terminal settings


define:
#!/bin/bash 
# program define
# version 1.0  
# written by Andrew V. Newman
# last modified Fri Mar  5 07:27:17 MST 2004

# Shell script to bring up google based definitions for any compound or simple word 

# Checks to see if you have a word in mind or just typing in the command. If the latter,
# it will give you a short usage and example before exiting with error code 1. 
if [ ${#} -lt "1" ]; then
   echo "Usage: `basename $0` 'TRM' "
   echo "   where TRM is the word that you would like defined"
   echo "   Examples:"
   echo "     `basename $0` Fedora"
   echo "     `basename $0` shell script"
   echo "    or:"
   echo "     `basename $0` google"
   exit 1
fi

# Use 'resize' to correctly set the environmental variables for your terminal 
# (this may not work for all terminals). 
  eval `resize -u` 

# Set the lynx program to your current term width (doesn't do it by 
# default if being piped directly into another file), and turn off the 
# default justification (can make output UGLY). 
  LYNX="/usr/bin/lynx -justify=0 -width=$COLUMNS"

# Set your 'more' or 'less' favorite pager.
  PAGER=/bin/more
  PAGER=/usr/bin/less

# Sets a URL BASE assuming multiple variables as a compound word.  The
# way WORD is defined, it will replace all the blank spaces with the URL
# equivalent of a blank '%20'. 
  WORD=` echo ${@} | sed  's/\ /\%20/g'`

# Define the google URL to search for the definition. 
  URL=http://www.google.com/search?q=define:$WORD 

# Call up the google search in lynx and dump the output to a temp file
# and all stderr to /dev/null . 
  $LYNX $URL -dump 2> /dev/null >define.tmp
  
# Displays definition after stripping off unnecessary text before and 
# after the definitions (the first sed command only prints lines 
# between the line containing 'Definition' and the line containing 
# '_____', inclusive.  Then it pipes it through a second sed command 
# which replaces the 'lynx' URL numbers with 'WEB:', before piping it 
# through a third sed command that wipes out that last line.  Finally
# the information is piped to a pager command which lets you page 
# through the text on-screen with the space bar. 
  sed -n '/[dD]efinition/,/_____/p' define.tmp | \
     sed '/\[[0-9][0-9]\]/s//   WEB: /' |\
     sed '/____/d' | $PAGER

# Remove temporary file. 
  rm define.tmp
# Exit cleanly. 
  exit 0

It may be easier to just download a copy (right click).
Once you have a copy on your system, all you need to do is:

  • Modify the first two variables to create your own defaults.
  • Change permissions of the file so that you can execute it:
    % chmod 755 define
  • Move file to a directory in your path:
    I would suggest creating a bin directory from your home for all shells and then making certain it is included in your path. % mkdir ~/bin
    % mv define ~/bin/
    If it is not already there, add the following line to ~/.cshrc if you are using a csh or tcsh shell path = ( $path ~/bin) Or, if you are using bash add the following line to ~/.bashrc PATH=$PATH:~/bin

I must say, there are other very wonderful and more robust dictionary tools available for Linux, including graphical 'kdict', and 'gnome-dictionary' which are both front-ends to 'dict'. The advantage to 'define' and 'dict' are that you can run them straight from the command line and you do not need to worry about bringing up another window which can slow you down, especially if you are plugged in remotely. I would not say that 'define' is in any way better than 'dict', it just accesses another dictionary and may give you a definition more suiting your needs.

OK, with that said, try out the examples in the header:

% define Fedora
% define shell script
% define google

Please email me if you found this useful, or have any comments or suggestions!