Command Line Currency Converter For Linux

On March 19, 2010, in LINUX, by segfault
1,575 views
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

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- "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 :-)

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Bookmarks
  • Twitter
  • Blogosphere News
  • FriendFeed
  • FSDaily
  • Yahoo! Buzz

Related posts:

  1. PDF Manipulations And Conversions From Linux Command Prompt
Tagged with:  

8 Responses to “Command Line Currency Converter For Linux”

  1. [...] rest is here: Command Line Currency Converter For Linux « Core Dump Posted in Uncategorized | Tags: entry, friday, linux, posted-on-friday, [...]

  2. handband2 says:

    How about a script like the following:

    #!/bin/bash
    ## http://www.google.com/finance/converter

    AMOUNT=`zenity --width=250 --title='Currency Converter' --text='Please enter US dollar (USD) amount' --entry`

    if [ "${AMOUNT}" == "" ]; then
    zenity --error --text="Amount not defined by user.
    Exiting. "
    exit 1
    fi

    (echo "European Euro (EUR)"; wget -qO- "http://www.google.com/finance/converter?a="$AMOUNT"&from=usd&to=eur" | sed '/res/!d;s/]*>//g';
    echo "Brazilian Real (BRL)"; wget -qO- "http://www.google.com/finance/converter?a="$AMOUNT"&from=usd&to=brl" | sed '/res/!d;s/]*>//g';
    echo "Mexican Peso (MXN)"; wget -qO- "http://www.google.com/finance/converter?a="$AMOUNT"&from=usd&to=mxn" | sed '/res/!d;s/]*>//g';
    echo "Argentine Peso (ARS)"; wget -qO- "http://www.google.com/finance/converter?a="$AMOUNT"&from=usd&to=ars" | sed '/res/!d;s/]*>//g';) | zenity --width=300 --height=300 --text-info --title "Converted Currency"

  3. [...] Excerpt from: Command Line Currency Converter For Linux « Core Dump [...]

  4. [...] more: Command Line Currency Converter For Linux « Core Dump ???????: ?????? ??????? | ?????: 2-0-feed-, entry, friday, linux, [...]

  5. matelot says:

    I’d rather just go to the web page

  6. [...] Command Line Currency Converter For Linux [...]

  7. [...] Command Line Currency Converter For Linux « Core Dump [...]

Leave a Reply