//View Tip #821
Similar Tips
» Change extension of files in the current directory
» list the most recent files in a directory
» Tar remote dir over SSH
» Vi mode in bash
» Change extension on a group of files using only bash builtins an

 

Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
The script below can be used to convert between different currencies on the command line. In order to use the script, you would enter something like "[scriptname] 150 USD GBP" to give the value of 150 US dollars in British pounds.

Supported currencies are Euro, U.S. dollar British pound, Japanese yen, Swiss franc, Canadian dollar, Australian dollar, and Indian rupee.

#!/bin/bash

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

if [ $# -eq 2 ]
then
  NUM=1;CURRENCY1=$(toUpper "$1"); CURRENCY2=$(toUpper "$2")
elif [ $# -eq 3 ]
then
  NUM=$1;CURRENCY1=$(toUpper "$2"); CURRENCY2=$(toUpper "$3")
else
  echo "Usage: $0 [number] currency1 currency2"
  echo "Ex: $0 100 EUR USD"
  echo "Available currencies: EUR, USD, GBP, JPY, CHF, CAD, AUD, INR"
  exit 1
fi

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
  RESULT=$(echo $CONVERSION \* $NUM | bc)
  echo "$NUM $CURRENCY1 = $RESULT $CURRENCY2"
fi

exit 0


View Comments »




Comments 

Add your comment

Comments are currently disabled
Robert Grønning
Google is capable of doing currency conversion directly.

Valid currency conversion querys using google.com:
* 132 usd to nok
* 132 dollars to norwegian kroner
* how much is 132 dollars in norwegian kroner?

http://www.google.com/search?q=how+much+is+132+dollars+in+norwegian+kroner
Posted 2009-05-27 11:05:56
sweet! I gave my script the name "$", so now I type "$ 3 dkk rub" (as
you can see, there are a lot more available currencies...)
Posted 2009-05-27 12:48:35

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit