#!/bin/bash # convertcurrency--Given an amount and base currency, convertw it to the # specified target currency using ISO currency identifiers. # Uses Google's finance converter for the heavy lifting: # http://www.google.com/finance/converter?a=1&from=USD&to=GBP if test $Argc -eq 0 { echo "Usage: $[basename $0] amount currency to currency" echo "Most common currencies are CAD, CNY, EUR, USD, INR, JPY, and MXN" echo "Use \"$[basename $0] list\" for the full list of supported currencies" } if test $[uname] = "Darwin" { setglobal LANG = 'C' # For an issue on Macs with invalid byte sequences and lynx } setglobal url = '"https://www.google.com/finance/converter'" setglobal tempfile = ""/tmp/converter.$Pid"" setglobal lynx = $[which lynx] # Since this has multiple uses, let's grab this data before anything else. setglobal currencies = $[$lynx -source $url | grep "option value=" | \ cut -d'"' -f2- | sed 's/">/ /' | cut -d'(' -f1 | sort | uniq] ########### Deal with all non-conversion requests. if test $Argc -ne 4 { if test $1 = "list" { # Produce a listing of all currency symbols known by the converter. echo "List of supported currencies:" echo $currencies } exit 0 } ########### Now let's do a conversion. if test $3 != "to" { echo "Usage: $[basename $0] value currency TO currency" echo "(use \"$[basename $0] list\" to get a list of all currency values)" exit 0 } setglobal amount = $1 setglobal basecurrency = $[echo $2 | tr '[:lower:]' '[:upper:]] setglobal targetcurrency = $[echo $4 | tr '[:lower:]' '[:upper:]] # And let's do it--finally! $lynx -source "$url?a=$amount&from=$basecurrency&to=$targetcurrency" | \ grep 'id=currency_converter_result' | sed 's/<[^>]*>//g' exit 0