-
Notifications
You must be signed in to change notification settings - Fork 0
/
60-convertcurrency
55 lines (41 loc) · 1.65 KB
/
60-convertcurrency
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/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 [ $# -eq 0 ]; then
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"
fi
if [ $(uname) = "Darwin" ]; then
LANG=C # For an issue on Macs with invalid byte sequences and lynx
fi
url="https://www.google.com/finance/converter"
tempfile="/tmp/converter.$$"
lynx=$(which lynx)
# Since this has multiple uses, let's grab this data before anything else.
currencies=$($lynx -source "$url" | grep "option value=" | \
cut -d\" -f2- | sed 's/">/ /' | cut -d\( -f1 | sort | uniq)
########### Deal with all non-conversion requests.
if [ $# -ne 4 ] ; then
if [ "$1" = "list" ] ; then
# Produce a listing of all currency symbols known by the converter.
echo "List of supported currencies:"
echo "$currencies"
fi
exit 0
fi
########### Now let's do a conversion.
if [ $3 != "to" ] ; then
echo "Usage: $(basename $0) value currency TO currency"
echo "(use \"$(basename $0) list\" to get a list of all currency values)"
exit 0
fi
amount=$1
basecurrency="$(echo $2 | tr '[:lower:]' '[:upper:]')"
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