Bring the Google Calculator to the Command Line
Perform feats of calculation on the command line, powered by the magic of the Google calculator.
Everyone, whether they
admit it or not, forgets how to use the
Unix dc
command-line calculator a few moments
after they figure it out for the nth time and stumble through the
calculation at hand. And, let’s face it, the default
desktop (and I mean computer desktop) calculator
usually doesn’t go beyond the basics: add, subtract,
multiply, and divide—you’ll have some grouping
ability with clever use of M+, M-, and MR, if you’re
lucky.
What if you’re interested in more than simple math? I’ve lived in the U.S. for years now and still don’t know a yard from three feet (I know now, thanks to the Google Calculator), let alone converting ounces to grams or stone to kilograms.
This two-line PHP script by Adam Trachtenberg (http://www.trachtenberg.com) brings the Google calculator to your command line so that you don’t have to skip a beat—or open your browser—when you just need to calculate something quickly.
The Code
The script uses PHP (http://www.php.net), better known as a web programming and templating language, on the command line, passing your calculation query to Google, scraping the returned results, and dropping the answer into your virtual lap.
Tip
This hack assumes that you have PHP installed on your computer and it lives in the /usr/bin directory. If PHP is somewhere else on your system, you should alter the path on the first line accordingly ...
Get Google Hacks, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.