Bash script to define words

Discussion in 'Mac Programming' started by noahgolm, Dec 7, 2010.

  1. noahgolm macrumors member

    Aug 18, 2010
    Hi, I;m trying to make a simple bash script to google the definition of a word (, 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:
    echo 'Enter a word to define: '
    read F1
    F=`echo $F1 | sed s/\ /+/g`
    open "$F"
    Any suggestions on reading the html, accessing the right words, and displaying it as plain text?
  2. noahgolm thread starter macrumors member

    Aug 18, 2010
    Never mind, found some helpful advice elsewhere, but you do need html2text from macports...
    Anyways, here's the code:
    # 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'  ''$word \
    | html2text -ascii -nobs -style compact -width 500 | grep "*" | head -n 1 | tail -n 1
  3. seepel macrumors 6502


    Dec 22, 2009
    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.
  4. Dalton63841 macrumors 65816


    Nov 27, 2010
    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?

Share This Page