#!/bin/bash set -e if [ $# = 0 ] ; then exit 0 fi from=${1:0:3} to=${1:3:3} get_value() { curl -s "http://www.xe.com/currencyconverter/convert/?Amount=1&From=$from&To=$to" | \ grep -o "[^<]\+" | \ grep -o '[0-9\.]\+' } get_value_at_date() { date=$(date -d "$1" +%F) curl -s "http://www.xe.com/currencytables/?from=$from&date=$date" | \ xmllint --nonet --html --xpath "//table[@id='historicalRateTbl']/tbody/tr[td/a/text() = '${to^^}']/td[3]/text()" - 2>/dev/null } if [ -z "$2" ] ; then val=$(get_value) else val=$(get_value_at_date "$2") fi printf '%.4f\n' $val