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

前端 未结 29 1770
慢半拍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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 07:02

    function version { echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; }
    

    Used as such:

    if [ $(version $VAR) -ge $(version "6.2.0") ]; then
        echo "Version is up to date"
    fi
    

    (from https://apple.stackexchange.com/a/123408/11374)

提交回复
热议问题