Extract version number from file in shell script

后端 未结 8 1419
醉话见心
醉话见心 2020-12-24 13:04

I\'m trying to write a bash script that increments the version number which is given in

{major}.{minor}.{revision}

For example.

<         


        
8条回答
  •  再見小時候
    2020-12-24 13:05

    Inspired by the answer of jlliagre I made my own version which supports version numbers just having a major version given. jlliagre's version will make 1 -> 1.2 instead of 2.

    This one is appropriate to both styles of version numbers:

    function increment_version()
        local VERSION="$1"
    
        local INCREMENTED_VERSION=
        if [[ "$VERSION" =~ .*\..* ]]; then
            INCREMENTED_VERSION="${VERSION%.*}.$((${VERSION##*.}+1))"
        else
            INCREMENTED_VERSION="$((${VERSION##*.}+1))"
        fi
    
        echo "$INCREMENTED_VERSION"
    }
    

    This will produce the following outputs:

    increment_version 1         -> 2 
    increment_version 1.2       -> 1.3    
    increment_version 1.2.9     -> 1.2.10 
    increment_version 1.2.9.101 -> 1.2.9.102
    

提交回复
热议问题