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

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

Get every new post delivered to your Inbox.