バージョン番号比較

こちらの StackOverflow の回答 をもとに、よくあるComparableっぽいインタフェースにしました。

実行例

$ while read -r test
  do
      vercomp $test
  done << EOF
  1            1
  2.1          2.2
  3.0.4.10     3.0.4.2
  4.08         4.08.01
  3.2.1.9.8144 3.2
  3.2          3.2.1.9.8144
  1.2          2.1
  2.1          1.2
  5.6.7        5.6.7
  1.01.1       1.1.1
  1.1.1        1.01.1
  1            1.0
  1.0          1
  1.0.2.0      1.0.2
  1..0         1.0
  1.0          1..0
  EOF

0
-1
1
-1
1
-1
-1
1
0
0
0
0
0
0
0
0

 関数定義

#
# These functions are based on:
# Bash. How compare two strings in “version” format
# http://stackoverflow.com/a/4025065
#
__vercomp () {
    if [[ $1 == $2 ]]
    then
        return 0
    fi
    local IFS=.
    local i ver1=($1) ver2=($2)
    # fill empty fields in ver1 with zeros
    for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
    do
        ver1[i]=0
    done
    for ((i=${#ver2[@]}; i<${#ver1[@]}; i++))
    do
        ver2[i]=0
    done
    for ((i=0; i<${#ver1[@]}; i++))
    do
        if [[ -z ${ver2[i]} ]]
        then
            # fill empty fields in ver2 with zeros
            ver2[i]=0
        fi
        if ((10#${ver1[i]} > 10#${ver2[i]}))
        then
            return 1
        fi
        if ((10#${ver1[i]} < 10#${ver2[i]}))
        then
            return 2
        fi
    done
    return 0
}

vercomp () {
    __vercomp $1 $2
    case $? in
        0) op='=';;
        1) op='>';;
        2) op='<';;
    esac
    if [[ $op == '>' ]]; then
        echo 1
    elif [[ $op == '<' ]]; then
        echo -1
    else
        echo 0
    fi
}