Posts Tagged ‘finance’

Command Line Currency Converter For Linux

March 19th, 2010 2 comments

currency-convertMany times I prefer to just work from the shell. The ~/.bashrc file is a great place to put functions if you are using them regularly.

Let’s build a cool command line currency converter using google’s currency converter. You might require wget or curl and sed to do this.

Just run the following command in terminal.

wget -qO- "" |  sed '/res/!d;s/<[^>]*>//g';

This will convert 1 USD to INR (Indian Rupee).

1 USD = 45.4950 INR

What we are doing is, query google using wget and parse the output using sed.

You can use curl as well:

curl -s "" |  sed '/res/!d;s/<[^>]*>//g';

Isn’t that a long command. Lets create a cool bash function for the ease use:
Open you ~/.bashrc file in your favorite text editor and add the following lines and save.

currency_convert() {
  wget -qO- "$1&from=$2&to=$3&hl=es" |  sed '/res/!d;s/<[^>]*>//g';

Now Run:

$ currency_convert 10 usd inr
10 USD = 454.9500 INR

If you want to convert to more than one currencies at once, rewrite our function as bellow.

currency_convert() {
  for i in $3
    wget -qO- "$1&from=$2&to=$i&hl=es" |  sed '/res/!d;s/<[^>]*>//g';

Now run:

$ currency_convert 1 usd "inr gbp eur"
1 USD = 45.4950 INR
1 USD = 0.6603 GBP
1 USD = 0.7382 EUR

Happy currency converting :-)

Categories: LINUX Tags: ,