Currency Exchange
February 27, 2015
Instead of writing a program, the easiest way to complete the task is to use wget
to fetch the data and awk
to format it:
$ cat >currency
wget -qO- "http://rate-exchange.appspot.com/currency?from=$1&to=$2" |
awk -vAMT="$3" -F'[ ,]' '{printf "%.2f", $5 * AMT}'
CTRL-D
$ chmod +x currency
$ ./currency USD GBP 1000
644.24
The currency
command takes three parameters: the from currency, the to currency, and the from amount; currency is specified by standard three-letter codes. The flags to wget
specify quiet mode with output (that’s a capital-letter oh, not a zero) to standard output. Output from the wget
command looks like this:
{"to": "GBP", "rate": 0.64380199999999999, "from": "USD"}
The awk
command splits the output into fields; the fifth field is the conversion rate, and simple multiplication does the rest.
The trick here is the url that provides the data, which I found by googling for “currency exchange api”. With baseball season starting in a few weeks, I will soon be googling for “baseball standings api”.