为了账号安全,请及时绑定邮箱和手机立即绑定

如何比较两个字符串的点分隔版本格式在巴什?

如何比较两个字符串的点分隔版本格式在巴什?

临摹微笑 2019-07-10 14:52:17
如何比较两个字符串的点分隔版本格式在巴什?有没有办法比较bash上的这些字符串,例如:2.4.5和2.8和2.4.5.1?
查看完整描述

3 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

以下是一个纯Bash版本,它不需要任何外部实用程序:


#!/bin/bash

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=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

}


testvercomp () {

    vercomp $1 $2

    case $? in

        0) op='=';;

        1) op='>';;

        2) op='<';;

    esac

    if [[ $op != $3 ]]

    then

        echo "FAIL: Expected '$3', Actual '$op', Arg1 '$1', Arg2 '$2'"

    else

        echo "Pass: '$1 $op $2'"

    fi

}


# Run tests

# argument table format:

# testarg1   testarg2     expected_relationship

echo "The following tests should pass"

while read -r test

do

    testvercomp $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


echo "The following test should fail (test the tester)"

testvercomp 1 1 '>'

运行测试:


$ . ./vercomp

The following tests should pass

Pass: '1 = 1'

Pass: '2.1 < 2.2'

Pass: '3.0.4.10 > 3.0.4.2'

Pass: '4.08 < 4.08.01'

Pass: '3.2.1.9.8144 > 3.2'

Pass: '3.2 < 3.2.1.9.8144'

Pass: '1.2 < 2.1'

Pass: '2.1 > 1.2'

Pass: '5.6.7 = 5.6.7'

Pass: '1.01.1 = 1.1.1'

Pass: '1.1.1 = 1.01.1'

Pass: '1 = 1.0'

Pass: '1.0 = 1'

Pass: '1.0.2.0 = 1.0.2'

Pass: '1..0 = 1.0'

Pass: '1.0 = 1..0'

The following test should fail (test the tester)

FAIL: Expected '>', Actual '=', Arg1 '1', Arg2 '1'


查看完整回答
反对 回复 2019-07-10
?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

如果您有coreutils-7(在Ubuntu业力,但不是黄疸),那么您sort命令应该有一个-V选项(版本排序),您可以使用该选项进行比较:


verlte() {

    [  "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]

}


verlt() {

    [ "$1" = "$2" ] && return 1 || verlte $1 $2

}


verlte 2.5.7 2.5.6 && echo "yes" || echo "no" # no

verlt 2.4.10 2.4.9 && echo "yes" || echo "no" # no

verlt 2.4.8 2.4.10 && echo "yes" || echo "no" # yes

verlte 2.5.6 2.5.6 && echo "yes" || echo "no" # yes

verlt 2.5.6 2.5.6 && echo "yes" || echo "no" # no


查看完整回答
反对 回复 2019-07-10
?
慕森王

TA贡献1777条经验 获得超3个赞

可能没有普遍正确的方法来实现这一点。如果要比较debian包系统中的版本,请尝试dpkg --compare-versions <first> <relation> <second>.


查看完整回答
反对 回复 2019-07-10
  • 3 回答
  • 0 关注
  • 430 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信