Implementations/Libraries/Shell

From Geohashing

Shell Script Implementation

This implementation IS FULLY 30W-compliant.
#!/bin/sh -e

command -v md5 >/dev/null 2>&1 || md5() { md5sum | cut -c 1-32; }; [ -n "$1" ]
command -v GET >/dev/null 2>&1 || GET() { wget -qO - "$@"; }; [ "$2" -gt -30 \
] && { date -R >/dev/null 2>&1 && y='-d yesterday' || y='-v -1d'; }; d=$(GET \
carabiner.peeron.com/xkcd/map/data/$(date $y +%Y/%m/%d)); printf $(date $3 +%F-$d) |
md5 | sed 'y/abcdef/ABCDEF/; s/.\{16\}/.&LgPp/g' | dc -e "[$2]Sg [$1]Sg 16i" -

Pass your graticule's lat and long as the arguments to the script. (A third argument may be used to manipulate the date, if you know what you are doing. This is not useful for much more than getting Saturday's hash on Friday.) If something fails it will exit with a nonzero status.

This works with GNU/FreeBSD date, lwp or wget, and bash/ksh/ash, but not OpenBSD date or curl (which are missing some of the features abused here). It would theoretically work with fetch but I removed that for the sake of brevity. Suggestions are welcome, as well as reports of compatibility with other operating systems or implementations of dc.

Here is a contributed wrapper script that will set default co-ordinates for you and call the above implementation, then attempt to print the answer as a Google Maps URL. On OS X, it will launch the URL, but on other operating systems weird things might happen. It has been edited to assume that the first script is installed as "geohash".

#!/bin/sh

if [ $# -lt 2 ] ; then  # Put your default geo-coords here
    : ${LAT:="36"}
    : ${LON:="-122"}
else
    : ${LAT:="$1"}
    : ${LON:="$2"}
fi

showGeoHash()
{
    echo "${1} ${2}"
    echo "http://maps.google.com/maps?q=${1}+${2}"
                                                        ######## Comment all but one of the following lines.
    open "http://maps.google.com/maps?q=${1}+${2}"      # For Mac OS X.
    firefox "http://maps.google.com/maps?q=${1}+${2}"   # For other *nix with Firefox installed in $PATH.
}

showGeoHash $(geohash $LAT $LON)