scripts

Currency conversion script

In this time of crisis perhaps we need to pay more attention to financial issues. I am interested in converting some currencies so I have written a script that given two currencies, converts on to the other. The script searches the info on Google Finance’s page.

An example of its usage is:

$ ./currencies.sh EUR USD
1 EUR = 1.316 USD

The available currencies are:

  EUR - Euros
  USD - United State Dollar
  GBP - British Pound
  JPY - Japanese Yen
  CHF - Swiss Franc
  CAD - Canadian Dollar
  AUD - Australian Dollar
  INR - Indian Rupee

You only have to copy & paste the code below into a file named currencies.sh (or whatever)

#!/bin/bash

# currencies.sh
# 2009 - Rafa Casado - rafacas@commandliners.com

toUpper() {
        echo $@ | tr "[:lower:]" "[:upper:]"
}

numargs=$#

if [ $numargs -ne 2 ]
then
        echo "Usage: $0 currency1 currency2"
        echo "Ex: $0 EUR USD"
        echo "Available currencies:"
        echo "  EUR - Euros"
        echo "  USD - United State Dollar"
        echo "  GBP - British Pound"
        echo "  JPY - Japanese Yen"
        echo "  CHF - Swiss Franc"
        echo "  CAD - Canadian Dollar"
        echo "  AUD - Australian Dollar"
        echo "  INR - Indian Rupee"
        exit 1
fi

CURRENCY1=$(toUpper "$1")
CURRENCY2=$(toUpper "$2")

CONVERSION=`wget -nv -O - "http://finance.google.com/finance?q=$CURRENCY1$CURRENCY2" 2>&1 | \
        grep " 1 $CURRENCY1 " | \
        sed -e "s/^.*<span class=bld>&nbsp;\(.*\)&nbsp;$CURRENCY2.*$/\1/"`
if [ ${CONVERSION:-1} == "1" ]
then 
        echo "Network error"
else
        echo "1 $CURRENCY1 = $CONVERSION $CURRENCY2"
fi

exit 0

If you want to know a specific currency conversion everyday (at 5 pm, for example) you can change the last line of the script:

echo "1 $CURRENCY1 = $CONVERSION $CURRENCY2"

for this one:

mail -s "1 $CURRENCY1 = $CONVERSION $CURRENCY2" your@mail.com

and add a cron job like this:

0 17 * * * /path_to_script/currencies.sh EUR USD

1 Comment

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields