define: a google-based web dictionary on the command line

by Andrew Newman

One of a series of example shells:

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

# 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

# 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.

# 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. 

# 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

A copy of this script exists in /home/anewman/bin/define on terremoto

Once you have finished creating this file all you need to do is:

  • 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 geophysics
% define shell script
% define bash

Course Home | | Updated: Mon Sep 18 17:48:48 EDT 2006