Command Line Currency Converter For Linux
Many 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- "http://www.google.com/finance/converter?a=1&from=usd&to=inr&hl=es" | sed '/res/!d;s/<[^>]*>//g'; |
This will convert 1 USD to INR (Indian Rupee).
Output:
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 "http://www.google.com/finance/converter?a=1&from=usd&to=inr&hl=es" | 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- "http://www.google.com/finance/converter?a=$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 do wget -qO- "http://www.google.com/finance/converter?a=$1&from=$2&to=$i&hl=es" | sed '/res/!d;s/<[^>]*>//g'; done } |
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
Recent Comments