Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

noahgolm

macrumors member
Original poster
Aug 18, 2010
65
0
Hi, I;m trying to make a simple bash script to google the definition of a word (http://google.com/search?q=define:), and I want it to display the results in text, or even just the first result. So far, it'll just open up the search results page in my browser. Here's the script so far:
Code:
echo 'Enter a word to define: '

read F1
F=`echo $F1 | sed s/\ /+/g`

open "http://google.com/search?q=define:$F"

Any suggestions on reading the html, accessing the right words, and displaying it as plain text?
 
Never mind, found some helpful advice elsewhere, but you do need html2text from macports...
Anyways, here's the code:
Code:
#!/bin/bash
# Command line look up using Google's define feature - command line dictionary

echo "Type in your word:"
read word

/usr/bin/curl -s -A 'Mozilla/4.0'  'http://www.google.com/search?q=define%3A+'$word \
| html2text -ascii -nobs -style compact -width 500 | grep "*" | head -n 1 | tail -n 1
 
I would look into the Google Search AJAX API which you can actually access as a RESTFul API. You can get a nice JSON formatted top 8 results from this.
 
Don't hurt me too bad for this, as I've never done anything quite like this, but if you are on a Mac, wouldn't it be easier to have the script make a call to the Dictionary app, instead of Google?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.