Posts Tagged ‘scripting’

What is my IP address from the command line, revisited.

March 27th, 2012 6 comments

Last year I wrote about a little script I use to query for my current IP address from the Linux CLI. dyn’s checker has served me well over the years and has been rock solid stable, which is why I chose it.

A problem has come about however whereby it is returning a private IP address ( which it is taking from the X-Forwarded-For value, rather than ignoring it purely because of the fact that it is a private (internal) IP.

I was trying to find a way to scrape Google’s what is my ip page because it doesn’t suffer from the same bug, and in the process I came about

So here is my new script, in its entirety:

echo -e "\n$(curl -s\n"

If you visit, on the bottom half of the page you will find a handful of other information you could extract. Now let’s hope that sticks around for a while.

Categories: All, Linux

Ternary operator for bash scripting.

January 13th, 2012 2 comments

I was curious whether or not ternary operations are possible in bash or not. It turns out they are but only for numeric comparisons. So what if you wanted to compare whether a string is empty if you cannot use a string comparison (-Z or -n for example)? Use the string’s length.

As always with Linux, there’s a plethora of ways to do what I am doing below, so this is just an example for your reference:

function check_mounted () {
    local result=`df | grep $1`
    echo $((${#result} > 0 ? 1 : 0))
Categories: All, Linux

Relative path to absolute path in a bash shell.

January 13th, 2012 No comments

There are many ways to convert a relative path to an absolute path which can be accessed regardless of the current working directory, particularly useful for scripting. Later, I may make a post to demonstrate the various ways, but the easiest way to do this is to use the readlink utility which comes bundled with just about every Linux distribution with the exception of Debian based systems which use realpath instead.

$ pwd

$ readlink -f ./../mnt/.Skype

When you use readlink in a script and assign a resulting path to a variable, you should add the n flag (-fn) to suppress the trailing newline.

As you might have noticed, I needed to use this in a script to launch Skype but specify that I want to use the data files on my usb key as opposed to the default ~/.Skype location. This however was not working with relative paths and Skype would crash with something like:

$ FatalAssert(*out_ptr != ‘.’ && !strstr(out_ptr, “../”) && !strstr(out_ptr, “..\\”))
FatalAssert(*out_ptr != ‘.’ && !strstr(out_ptr, “../”) && !strstr(out_ptr, “..\\”))

The moment I pass a real path to Skype’s –dbpath parameter, everything works as expected.

Handy command line currency converter.

February 24th, 2010 4 comments

In 2003, I wrote a custom Amazon-like shopping cart script. Back then, my web scraping skills weren’t, ahem, the best they could have been. For the site’s currency conversions, I had a cron job pull down 3 sets of converstions, calculated the average for each currency pair, then stored the result locally for my use.

At the time, I did this hoping that in a worse-case-scenario, a maximum of 2 sources would die in any weekend. Luckily, the sources lasted for the 3 years whilst I was working for that company (and apparently for another two after that). Now, I hear, they are manually updating a static text file… once in a while.

Today, I have no such needs but as always I am constantly looking for new ways to do things, should the need arise. I also needed a simple currency converter for doing small calculations like how much to top up a prepaid credit card by to purchase something from eBay which is listed in another currency.

Full credits to rafacas at commandliners for the inspiration and as he points out, note that the script uses Google Finance’s page.


# 2009 - Rafa Casado -
# 2010 - Chris Ergatides -

if [ $# -lt 2 ]; then
    echo -e "\nUsage: $0 currency1 currency2 amount"
    echo "Default: $0 GBP EUR 1"
    echo "Example 1: $0 USD GBP"
    echo "Example 2: $0 GBP USD 42"

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

if [ -n "$1" ]; then FROM=$(toUpper "$1"); else FROM=GBP; fi
if [ -n "$2" ]; then TO=$(toUpper "$2"); else TO=EUR; fi
if [ $TO == $FROM ]; then echo 'Nothing to do!'; exit 2; fi
if [ -n "$3" ]; then A=$3; else A=1; fi


RESULT=`wget -nv -O - "$CONVERTER" 2>&1 | \
sed -n -e 's/.*<span class=bld>\(.*\)<\/span>.*/\1/p'`

echo -e "\nResult: $A $FROM = $RESULT\n"

So, let’s say you’ve named the script and made it executable with chmod +x, what does the output look like?

#Called without any parameters:
[[email protected] ~/bin]$ ./

Usage: /home/chris/bin/ currency1 currency2 amount
Default: /home/chris/bin/ GBP EUR 1
Example 1: /home/chris/bin/ USD GBP
Example 2: /home/chris/bin/ GBP USD 42

Result: 1 GBP = 1.1387 EUR
#Called with example 1's parameters:
[[email protected] ~/bin]$ ./ usd gbp

Result: 1 USD = 0.6475 GBP
#Called with example 2's parameters
[[email protected] ~/bin]$ ./ gbp usd 42

Result: 42 GBP = 64.8606 USD
Categories: All, Linux