computing
  • 0

Solved Comparing Decimal Number’s In Unix Shell Script

  • 0

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

Share

1 Answer

  1. 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

    • 0