How to compare versions of some products in unix ksh shell?

Pure Bash / Ksh:

compareVersions ()
{
  typeset    IFS='.'
  typeset -a v1=( $1 )
  typeset -a v2=( $2 )
  typeset    n diff

  for (( n=0; n<4; n+=1 )); do
    diff=$((v1[n]-v2[n]))
    if [ $diff -ne 0 ] ; then
      [ $diff -le 0 ] && echo '-1' || echo '1'
      return
    fi
  done
  echo  '0'
} # ----------  end of function compareVersions  ----------

Maybe you could use awk?

echo $VER_1 $VER2 | \
awk '{ split($1, a, ".");
       split($2, b, ".");
       for (i = 1; i <= 4; i++)
           if (a[i] < b[i]) {
               x =-1;
               break;
           } else if (a[i] > b[i]) {
               x = 1;
               break;
           }
       print x;
     }'

There isn't a perfect way to do this. As shown you could use array / loop for the numbers, also in bash.

Tags:

Unix

Shell

Ksh