Solved comparing Decimal number's in unix shell script

January 29, 2013 at 21:23:09
Specs: Windows XP

Hello,

I'm stuck on this issue where i need to compare Decimal number's in unix shell script.
Example:
Want to compare file versions
version of "File1" is 11.5.69
version of "File2" is 11.5.70

Now File2 is a newer version , and now if i compare File1 and File2 it should show me File2 is greater.

Kindly help me on this.

Thanks in advance.

Rajz


See More: comparing Decimal numbers in unix shell script

Report •


#1
January 29, 2013 at 23:33:35

11.5.69 is not a valid number; under unix it's considered a string. Consider deleting the periods before doing the comparison:

#!/bin/ksh

file1="11.5.69"
file2="11.5.70"

file1=$(echo $file1|sed 's/\.//g')
file2=$(echo $file2|sed 's/\.//g')

if [ $file2 -gt $file1 ]
then
   echo "File2 is greater"
else
   echo "File1 is greater"
fi


Report •

#2
February 5, 2013 at 23:30:13

Hello Nails,

Thank you for the reply, i shall consider this and try this out.

But then there would be a problem if the decimals would be deleted as in :

file1="11.5.69"
file2="1.15.70"

In the above case "file1" would be the latest version than "file2".

Please let me know if the below can be done:
I was having this thought , my file has the values

" $Header: OEXVHLSB.pls 115.10 2002/12/05 18:38:07 "

i'm using the below code to get only the file version:

fileversion=`adident Header $P_TOP/$f|tail -1|cut -f3,3 -d" "`
echo $fileversion

now would be fileversion=115.10

is there a possibility to get the date beside the version and compare the date, this will help me to get the latest file out of the two.


Kindly help me on this.


Thanks in advance.
Rajz


Report •

#3
February 6, 2013 at 10:27:42
✔ Best Answer

First:

Since the version i.e.11.5.70 is not a valid number, I think you'll have to break each file's version number into 3 tuplets and compare each one. It's a kludge, but here is one way to do it:

#!/bin/ksh

file1="11.5.69"
file2="1.5.70"

# break file1 into $1, $2, $3
set $(IFS="."; echo $file1)
f11=$1
f12=$2
f13=$3

# break file2 into $1, $2, $3
set $(IFS="."; echo $file2)

while true
do

   if [ $1 -eq $f11 ]
   then
      :
   else
      if [ $1 -gt $f11 ]
      then
         echo "File2 is greater"
      else
         echo "File1 is greater"
      fi
      break
   fi

   if [ $2 -eq $f12 ]
   then
      :
   else
      if [ $2 -gt $f12 ]
      then
         echo "File2 is greater"
      else
         echo "File1 is greater"
      fi
      break
   fi

   if [ $3 -eq $f13 ]
   then
      echo "files are equal"
   else
      if [ $3 -gt $f13 ]
      then
         echo "File2 is greater"
      else
         echo "File1 is greater"
      fi
   fi

   break
done
# end script

Of course, the limitation is that each version has to be 3 tuplets.

Second:

I am not familiar with the command adident, so I cannot comment on that. However, you can also get the version from the included string using awk; it's the 3rd field:

echo " $Header: OEXVHLSB.pls 115.10 2002/12/05 18:38:07 " |awk ' { print $3 } '

You can also get the date and time string; it's the 4th and 5th fields:

echo " $Header: OEXVHLSB.pls 115.10 2002/12/05 18:38:07 " |awk ' { print $4 $5 }
'
Including a datetime comparison is not simple; IMO, it involves changing the datetime to the number of seconds since the unix epoch, Jan 1, 1970, which isn't easy using classic unix tools (i.e. non-GNU tools). To perform epoch calculations, I use perl. Here is an example post:

http://www.computing.net/answers/pr...

Date conversion has been discussed a lot on computing.net. Perform this google for many examples:

site:computing.net epoch


Report •

Related Solutions

#4
February 7, 2013 at 00:06:32

Hello Nails,

Thank you for the suggestion, i shall try this out.
Regarding the breaking of the version is, yes that would of course be the limitation. As we dont know what would be the version.

I think i'll also have to compare Date, i'm trying ways to compare the dates as i'm not aware of how to go on with this.

I have gone thru your link, please let me know you have any suggestions.

Thank you,
Rajz


Report •

#5
February 7, 2013 at 20:47:06

As long as your datetime string is of the form:

YYYYMMDD HHMMSS

you should be able to compare the different strings.

If you have any specific questions, let me know.


Report •


Ask Question