Using Google as Calculator for Bash

In my eyes, GNU’s program “expr” is not very handy. Google has its own build-in calculator with currency-, unit conversion and further tools. So I’ve wrote a tiny bash-script to use Google for my conversions and calculations:

Update: Narf, pasting code in wordpress is not a fine thing. So here’s the paste: http://sprunge.us/BNeX
Update2: Henrik got rid of the HTML-Output by an unvalid query. Here’s the updated version.

#!/bin/bash
# Written by Matthias Brandt (http://www.matthias-brandt.de)
# No warrenty given. Use may violent against Google's Terms of Use.
# Copyleft: GPLv3, http://www.gnu.org/copyleft/lgpl.html

USER_AGENT=’Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9) Gecko/2008061017 Firefox/3.0′

#Substitute “+”,”/” with HTML-entities and blanks with “+”
QUERY=$(echo $* | sed ‘s/+/\%2B/g;s/\//\%2F/g;s/ /+/g’)

#wget the page, find the result and append a newline
wget -q -O – –user-agent=$USER_AGENT http://www.google.de/search?q=”$QUERY” | sed -e ‘/img/!d;s/.*<td nowrap dir=”ltr”><h2 class=”r”><font size=”+1″><b>\([^<]*\).*/\1/;/</d’ && echo “”

Examples:
calc 3 euro in usd
3 Euro = 4,6803 US-Dollar

calc 4353 in hex
4 353 = 0×1101

calc “4*3^2*sqrt(2)*tan(0.5)”
4 * (3^2) * sqrt(2) * tan(0.5) = 27.8131821

Advertisement
Published in: on July 24, 2008 at 4:57 pm  Comments (2)  
Tags: , , , ,

The URI to TrackBack this entry is: http://mattelacchiato.wordpress.com/2008/07/24/using-google-as-calculator-for-bash/trackback/

RSS feed for comments on this post.

2 CommentsLeave a comment

  1. Nice script. But try the following:
    calc 5 euro in franken
    ‘franken’ is not a valid keyword and so you get a lot of html output…

  2. calc 5 euro in chf is the query, you’re looking for ;-)

    I’ve deleted all lines angle brackets, so I hope this works. The whole script is more a hack than real coding :-)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.