Tuesday, September 23, 2008

Find Your Version of glibc

This is how you can find the version of glibc installed on your Linux machine:
sharfah@starship:~> rpm -q glibc
glibc-2.3.3-98.73

Thursday, September 04, 2008

Tuesday, September 02, 2008

How to Find Your Solaris Version

If you are developing an application which runs on Solaris, you may need to know which version of Solaris it is, so that you can use the correct JRE build, Sybase libraries etc.

You can do this using the uname command which prints out useful system information. In order to get just the version number (release level) use the -r flag:

sharfah@starship:~> uname -r
5.10
Example:
OS=`uname`
OS_VERSION=`uname -r`

if [ "$OS" = "SunOS" ]
then
 JAVA=/utils/solaris/java_${OS_VERSION}/bin
elif [ "$OS" = "Linux" ]
then
 JAVA=/utils/linux/java/bin
else
 echo "Unknown operating system $OS"
fi
If you want more details on your Solaris version, look at /etc/release.
sharfah@starship:~> cat /etc/release
                        Solaris 10 8/07 s10x_u4wos_12b X86
           Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
                        Use is subject to license terms.
                            Assembled 16 August 2007

Saturday, August 23, 2008

Eclipse Shirt Is Here!

FedEx dropped off the much anticipated Eclipse t-shirt today and I must say, it looks fab! I got it as a result of taking part in the Ganymede Around the World Contest. My coder friends are going to be so jealous!

I've posted a few pictures of the t-shirt here. Enjoy!


Front
Back
Eclipse Logo
Eclipse
Sleeve

Related post:
Here is my review of Ganymede, for those who missed it: Eclipse Ganymede Review

Monday, August 18, 2008

Subtract Two Dates [Unix]

In this post, I will describe how you can subtract two dates (which are in the form yyyyMMdd) to get the time between them. My approach involves first parsing both dates in order to extract the year, month and day, converting them to seconds using floating point math and finally subtracting one from the other.

Bash doesn't support floating point arithmetic, but I won't let that stop me. I will use awk instead. Another possible candidate is bc.

1. Parsing the date
We need to extract the year, month and day from our date which is in yyyyMMdd e.g. 20080818. The year is composed of the first four characters, the month is the next two and the day, the final two. We can do this using awk's substr function as shown below. substr(s,m,n) returns the n-character substring of s that begins at position m.

today=20080818
echo $today | awk '{
 year=substr($1,1,4);
 month=substr($1,5,2);
 day=substr($1,7,2);
 }'
If you don't like awk, you can also get substrings using the shell's expr command:
today=20080818
year=`/usr/ucb/expr substr $today 1 4`
month=`/usr/ucb/expr substr $today 5 2`
day=`/usr/ucb/expr substr $today 7 2`
2. Converting to seconds
We will use the formula below to give us the number of days since 1/1/1970:
(year-1970)*365.25 + month*30.5 + day

Now convert the days to seconds by multiplying the days by 24 * 60 * 60:
((year-1970)*365.25 + month*30.5 + day) * 24 * 60 * 60

3. Subtracting the times
Once we have both dates in seconds, we can subtract them. Since we're still dealing with floating point we should use awk or bc for precision.
echo $seconds1 $seconds2 | awk '{print $1 - $2}'
or
echo $seconds1 - $seconds2 | bc
Putting it all together
Here is the complete shell script which takes two dates and returns the number of seconds between them:
#!/usr/bin/bash

date1=$1
date2=$2

#give the dates to awk
echo $date1 $date2 | awk '{

#parse
year1=substr($1,1,4);
month1=substr($1,5,2);
day1=substr($1,7,2);

year2=substr($2,1,4);
month2=substr($2,5,2);
day2=substr($2,7,2);

#get seconds
secs1=((year1 - 1970)*365.25+(month1*30.5)+day1)*24*60*60;
secs2=((year2 - 1970)*365.25+(month2*30.5)+day2)*24*60*60;

#subtract
print secs1 - secs2;
}'
As you can see, all of the computation is in awk! So we could put all of our awk code into a separate file called subtractDates.awk, for instance, and run it on the command line like this: echo 20080830 20080818 | awk -f subtractDates.awk

Now that we have the number of seconds between two dates, we can convert that to days, months and years using a similar approach if we have to.