#!/bin/sh
##################
# Version 2.
# Converts from any rate to any rate.
#
# * Now requires the first parameter to be the file name that features all
# currencies in '<3-letter currency> <rate>' (per line) format.
# * 'list' enhanced to deal with large currency quantities an additional
# argument is now also supported to list, which is the initial string of
# the currency name, thus 'list a' lists all currencies startign with 'a'
#
RATES="$1"
#RATES="/home/qbcdast/curr/rates"
amount=$2
fromcurr=$3
tocurr=$4
fromname=$5
toname=$6
fromcountry=$7
tocountry=$8
if test "list" = "$amount"; then
cat "$RATES" | sort | grep -i "^$fromcurr" | awk 'BEGIN{a=""; c=0;} {c++; a = sprintf("%s%s ", a, $1); if(length(a)>240) { print a; a=""; } } END { if(length(a))print a; print c " currencies!";}'
exit
fi
fromrate=`grep -i "$fromcurr" "$RATES" | awk '{ print $2;}'`
torate=`grep -i "$tocurr" "$RATES" | awk '{ print $2;}'`
if test -z "$torate"; then
echo "*** I have no rate for $tocurr"
exit
fi
if test -z "$fromrate"; then
echo "*** I have no rate for $fromcurr"
exit
fi
# This bc-line is to make the actual calculation use a 99-scale, while
# we want the output to use only 3!
result=`echo "scale=99; a= $amount / ($fromrate/$torate); scale=3; a/1" | bc`
echo "$amount $fromcurr ($fromcountry $fromname) makes $result $tocurr ($tocountry $toname)"
syntax highlighted by Code2HTML, v. 0.9.1