The aim of this tutorial is to show currency exchange rates and reports as a welcoming note whenever you open your Linux computer or server. You might not always remember to check the online charts, so this tool will always remind you of the current exchange rates.

For ubuntu here, I’ll be using notify-send command which is very useful for showing custom desktop notification bubbles.

notify-send Notification Title Notification Message

I am not sure if this command is supplied in other linux distros, you can find that out yourself for your favorite distribution or just print out the currency report message in the terminal.

We’ll be scraping a public service XE.com that offers currency exchange rates for many currency pairs, and match the rate value holder (which is a SPAN tag for this site) using Perl regular expression. I know it is not cool to scrape a service, but since we’re only doing it say like once or twice a day, that’s nearly no impact on their site. But there are certainly many free APIs out there which you can use instead.

Tweaking bash start

We’ll want to create a shell file and store it somewhere in the server, which will contain the code necessary to fetch a currency pair exchange rate and show it to the user.

For me, I placed the file as ~/currency:

#!/usr/bin/env sh
getCurrencyExchangeRate(){
    # $1 = base (from) currency
    # $2 = target (lookup, to) currency
    if ! [ -z "$1" ] && ! [ -z "$2" ]; then
        uri="http://www.xe.com/currencyconverter/convert/?Amount=1&From=$1&To=$2"
        value=`curl -s --connect-timeout 60 --max-time 60 $uri | perl -l -0777 -ne 'print $3 if /<span class=(.*?)uccResultAmount(.*?)>\s*(.*?)\s*<\/span>/si'`

        if ! [ -z "$value" ]; then
            notify-send "CURRENCY REPORT" "1 $1 = $value $2"

            # or if you want to output directly to the terminal
            # remove the `> /dev/null 2>&1 &` part in the usage
            # note that it'll become blocking until the curl is
            # done, so maybe reduce 60 timeout if you have slow
            # internet connection (as me), otherwise no worries
            # printf "\033[0;33m== CURRENCY REPORT ==\033[0m\n"
            # echo "1 $1 = $value $2"
        fi
    fi
}

You now have declared the getCurrencyExchangeRate function, it expects 2 arguments:

  1. The base currency, which we’ll be converting from.
  2. The target currency, to which we’ll be converting the first currency.

An example: converting from USD to MAD:

getCurrencyExchangeRate USD MAD

Another example: converting from BTC (bitcoin) to USD:

getCurrencyExchangeRate XBT MAD

Please consult XE.com or the API/site you’re using to verify the currency unique code.

Next, you’ll want to modify the ~/.bashrc file to load this file where we declared the currency fetching function. Open it with your favorite editor, and add

. ~/currency

to the end in a new line, and save the file. Make sure to replace ~/currency with the correct file path if you have a different one.

That’s going to load the target file every time a new bash session has started.

Extracting Currency Rates

Yet, we haven’t used the currency fetcher function, we’ll need to go back and edit the file ~/currency, to add some custom code:

taskRunner(){
    getCurrencyExchangeRate USD MAD
}

taskRunner > /dev/null 2>&1 &

That’s going to run the currency fetcher in the background, so we would not worry about our terminal taking so long to accept user input, as curl will be blocking until it actually downloads the currency page (or API response).

For multiple currency pair fetching, you can just add multiple function calls, with some delays between those calls, so we don’t exhaust the web service or API provider.

taskRunner(){
    getCurrencyExchangeRate USD MAD
    sleep 5
    getCurrencyExchangeRate XBT USD
}

taskRunner > /dev/null 2>&1 &

If you have slow internet connection like me most of the time (Morocco), then you might want to modify the CURL command to set the connection timeout to some small amount of seconds, that is if it is blocking your terminal, otherwise no worries.

Here’s a preview of the desktop notification bubbles holding the currency pair exchange rates (USD/MAD and BTC/USD in this example):

currency report usd-mad previewcurrency report btc-usd preview

Show on Terminal

You can make this output straight to the terminal when you don’t have a notifications tool, or when you want this to run in your server and show whenever you SSH into your machine, you just want to remove this line:

notify-send "CURRENCY REPORT" "1 $1 = $value $2"

and comment-out these lines:

# printf "\033[0;33m== CURRENCY REPORT ==\033[0m\
# echo "1 $1 = $value $2"

Oh, and don’t forget to remove this part > /dev/null 2>&1 & from the taskRunner callback, otherwise it’ll run in the background and the process output will be lost.

Preview:

currency exchange report - Show on Terminal

Conclusion

As a freelancer, I am always interested in finding out what is the conversion rate for my local currency compared to the currency I am getting paid with, so I can convert my money to the local currency when the rates are in good standing. What’s your use case? Share with us below 🙂

The full code used on this tutorial:

#!/usr/bin/env sh
getCurrencyExchangeRate(){
    # $1 = base (from) currency
    # $2 = target (lookup, to) currency
    if ! [ -z "$1" ] && ! [ -z "$2" ]; then
        uri="http://www.xe.com/currencyconverter/convert/?Amount=1&From=$1&To=$2"
        value=`curl -s --connect-timeout 60 --max-time 60 $uri | perl -l -0777 -ne 'print $3 if /<span class=(.*?)uccResultAmount(.*?)>\s*(.*?)\s*<\/span>/si'`

        if ! [ -z "$value" ]; then
            notify-send "CURRENCY REPORT" "1 $1 = $value $2"

            # or if you want to output directly to the terminal
            # remove the `> /dev/null 2>&1 &` part in the usage
            # note that it'll become blocking until the curl is
            # done, so maybe reduce 60 timeout if you have slow
            # internet connection (as me), otherwise no worries
            # printf "\033[0;33m== CURRENCY REPORT ==\033[0m\n"
            # echo "1 $1 = $value $2"
        fi
    fi
}

taskRunner(){
    getCurrencyExchangeRate USD MAD
    sleep 5
    getCurrencyExchangeRate XBT USD
}

taskRunner > /dev/null 2>&1 &
Digital Ocean

Cheap Cloud SSD Hosting

Get a VPS now starting at $5/m, fast and perfect for WordPress and PHP applications

Sign Up with $10 Credit