O'Reilly logo

Linux Shell Scripting Cookbook by Sarath Lakshman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

define utility with Web backend

Google provides Web definitions for any word by using the search query define:WORD. We need a GUI web browser to fetch the definitions. However, we can automate it and parse the required definitions by using a script. Let's see how to do it.

Getting ready

We can use lynx, sed, awk, and grep to write the define utility.

How to do it...

Let's go through the code for the define utility script to fetch definitions from Google search:

#!/bin/bash
#Filename: define.sh
#Description: A Google define: frontend

limit=0
if  [ ! $# -ge 1 ];
then
  echo -e "Usage: $0 WORD [-n No_of_definitions]\n"
  exit -1;
fi

if [ "$2" = "-n" ];
then
  limit=$3;
  let limit++
fi

word=$1
 lynx -dump http://www.google.co.in/search?q=define:$word | \ awk ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required