-
Notifications
You must be signed in to change notification settings - Fork 11
/
forex
109 lines (87 loc) · 3.32 KB
/
forex
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
#----------------------------------------------------------------------------------
# Project Name - miscellaneous/forex
# Started On - Sun 15 Oct 16:25:38 BST 2017
# Last Change - Sat 3 Mar 22:34:19 GMT 2018
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#----------------------------------------------------------------------------------
XERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; exit 1; }
[ -x /usr/bin/wget ] || XERR "$LINENO" "Dependency '/usr/bin/wget' not met."
DOM="https://www.foreignexchange.org.uk"
USAGE(){
while read -r; do
printf "%s\n" "$REPLY"
done <<-EOF
FOREX (3rd March 2018)
Written by terminalforlife ([email protected])
Easily convert various currency rates straight from the terminal.
SYNTAX: forex [OPTS] SOURCE AMOUNT TARGET
OPTS: --help|-h|-? - Displays this help information.
--debug|-D - Enables the built-in bash debugging.
--rounded|-R - Do not display as a floating point.
EXAMPLE: $ forex USD 5 GBP
0.7254
SITE: $DOM
EOF
}
while [ "$1" ]; do
case "$1" in
--help|-h|-\?)
USAGE; exit 0 ;;
--debug|-D)
DEBUGME="true" ;;
--rounded|-R)
ROUNDED="true" ;;
-*)
XERR "$LINENO" "Incorrect argument(s) specified." ;;
*)
break ;;
esac
shift
done
SOURCE="$1"
AMOUNT="$2"
TARGET="$3"
# Newer versions of wget by default request unwanted server-side compression.
read -a WGET_VER_LINE <<< "$(/usr/bin/wget --version)"
[ 0${WGET_VER_LINE[2]//[!0-9]} -ge 01192 ] && NOWARC="--no-warc-compression"
[ "$DEBUGME" == "true" ] && set -xeu
# Curent and historical currency abbreviations. Taken from: http://www.xe.com
declare -a VALID=(
USD EUR GBP INR AUD CAD SGD CHF MYR JPY CNY NZD THB HUF AED HKD MXN ZAR PHP
SEK IDR SAR BRL TRY KES KRW EGP IQD NOK KWD RUB DKK PKR ILS PLN QAR XAU OMR
COP CLP TWD ARS CZK VND MAD JOD BHD XOF LKR UAH NGN TND UGX RON BDT PEN GEL
XAF FJD VEF BYN HRK UZS BGN DZD IRR DOP ISK XAG CRC SYP LYD JMD MUR GHS AOA
UYU AFN LBP XPF TTD TZS ALL XCD GTQ NPR BOB ZWD BBD CUC LAK BND BWP HNL PYG
ETB NAD PGK SDG MOP NIO BMD KZT PAB BAM GYD YER MGA KYD MZN RSD SCR AMD SBD
AZN SLL TOP BZD MWK GMD BIF SOS HTG GNF MVR MNT CDF STD TJS KPW MMK LSL LRD
KGS GIP XPT MDL CUP KHR MKD VUV MRO ANG SZL CVE SRD XPD SVC BSD XDR RWF AWG
DJF BTN KMF WST SPL ERN FKP SHP JEP TMT TVD IMP GGP ZMW
)
for TYPE in ${VALID[@]}; {
[ "$TYPE" == "$SOURCE" ] && SOURCE_YES="true"
[ "$TYPE" == "$TARGET" ] && TARGET_YES="true"
}
if ! [ "$SOURCE_YES" == "true" ]; then
XERR "$LINENO" "Currency SOURCE '$SOURCE' not recognised."
elif ! [ "$TARGET_YES" == "true" ]; then
XERR "$LINENO" "Currency TARGET '$TARGET' not recognised."
fi
[ $UID -eq 0 ] && XERR "$LINENO" "Root access isn't required."
SITE="$DOM/fx-rates/conversion/$AMOUNT/$SOURCE/$TARGET"
while read -a X; do
if [[ "${X[*]}" =~ name=\"answer\" ]]; then
RESULT="${X[3]//[!0-9.]}"
fi
done <<< "$(/usr/bin/wget -q -t 1 $NOWARC "$SITE" -O -)"
if [[ "$RESULT" =~ ^[0-9.]+$ ]]; then
if [ "$ROUNDED" == "true" ]; then
printf "%s\n" "${RESULT%.*}"
else
printf "%.4f\n" "$RESULT"
fi
else
XERR "$LINENO" "Unable to determine the correct result."
fi
# vim: noexpandtab colorcolumn=84 tabstop=8 noswapfile nobackup