Groovy script as a *nix command line utility
I was having some fun with Groovy the other day as I created quick and dirty simple script to find out the duration between 2 dates. I've always wanted a handy utility that could answer more or less precisely questions like 'how much time has passed since I bought my MacBook pro?' or 'how old was I when I moved to the United States?', etc.
So, without doing much research on what's available, I set out to use an excellent Joda time library in the Groovy script setting. And after 'shebanging' it (#!/usr/bin/env groovy) and installing it for example into /usr/sbin (provided that I have Groovy installed, $GROOVY_HOME set, and joda jar available ~/.groovy/lib), it's now available as a handy CLI utility where I could use it like this (how old was I when I started working at Rutgers): 'duration oct-9-1976 dec-9-2002' and the answer is: 26 years 2 months :-)
I like Groovy's seamless integration with Java and its libraries as well as 'low ceremony' approach such as simple script without 'main method noise' requirement, implicit availability of the args array, etc.!
There could be better ways to do this, but I had fun nevertheless :-)
Here's the script:
Later...

4 comments:
Very nice. Sometimes in life, it's the small things that really need fixing. This looks quite handy.
I agree Groovy is awesome. It quickly replaced Perl/Python/sh/bash for many of my "shell scripts" and all of my quick unix programs.
Sorry but groovy is the wrong tool. The vm startup time makes groovy unusable for scripting.
100% usable for me :-)
Cheers.
Post a Comment