How to compare two strings in dot separated version format in Bash?

前端 未结 29 1797
慢半拍i
慢半拍i 2020-11-22 06:52

Is there any way to compare such strings on bash, e.g.: 2.4.5 and 2.8 and 2.4.5.1?

29条回答
  •  半阙折子戏
    2020-11-22 07:22

    ### the answer is does we second argument is higher
    function _ver_higher {
            ver=`echo -ne "$1\n$2" |sort -Vr |head -n1`
            if [ "$2" == "$1" ]; then
                    return 1
            elif [ "$2" == "$ver" ]; then
                    return 0
            else
                    return 1
            fi
    }
    
    if _ver_higher $1 $2; then
            echo higher
    else
            echo same or less
    fi
    

    It's pretty simple and small.

提交回复
热议问题