//View Tip #821
Similar Tips
» Loop over files with spaces in the name
» Splitting arguments with read
» Change file extension revisited
» Create a Terminal Calculator
» Generate a random password


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.


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

if [ $# -eq 2 ]
  NUM=1;CURRENCY1=$(toUpper "$1"); CURRENCY2=$(toUpper "$2")
elif [ $# -eq 3 ]
  NUM=$1;CURRENCY1=$(toUpper "$2"); CURRENCY2=$(toUpper "$3")
  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

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" ]
  echo "Network error"
  RESULT=$(echo $CONVERSION \* $NUM | bc)

exit 0

View 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?

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