Extract version number from file in shell script

后端 未结 8 1361
醉话见心
醉话见心 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
    $ v=1.2.13
    $ echo "${v%.*}.$((${v##*.}+1))"
    1.2.14
    

    $ v=11.1.2.3.0
    $ echo "${v%.*}.$((${v##*.}+1))"
    11.1.2.3.1
    

    Here is how it works:

    The string is split in two parts.

    • the first one contains everything but the last dot and next characters: ${v%.*}
    • the second one contains everything but all characters up to the last dot: ${v##*.}

    The first part is printed as is, followed by a plain dot and the last part incremented using shell arithmetic expansion: $((x+1))

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-24 13:14

    Pure Bash using an array:

    version='1.2.33'
    a=( ${version//./ } )                   # replace points, split into array
    ((a[2]++))                              # increment revision (or other part)
    version="${a[0]}.${a[1]}.${a[2]}"       # compose new version
    
    0 讨论(0)
  • 2020-12-24 13:17

    Small variation on fgm's solution using the builtin read command to split the string into an array. Note that the scope of the IFS variable is limited to the read command (so no need to store & restore the current IFS variable).

    version='1.2.33'
    IFS='.' read -r -a a <<<"$version"
    ((a[2]++))
    printf '%s\n' "${a[@]}" | nl
    version="${a[0]}.${a[1]}.${a[2]}"
    echo "$version"
    

    See: How do I split a string on a delimiter in Bash?

    0 讨论(0)
  • 2020-12-24 13:18

    I use the shell's own word splitting; something like

    oIFS="$IFS"
    IFS=.
    set -- $version
    IFS="$oIFS"
    

    although you need to be careful with version numbers in general due to alphabetic or date suffixes and other annoyingly inconsistent bits. After this, the positional parameters will be set to the components of $version:

    $1 = 1
    $2 = 2
    $3 = 13
    

    ($IFS is a set of single characters, not a string, so this won't work with a multicharacter field separator, although you can use IFS=.- to split on either . or -.)

    0 讨论(0)
  • 2020-12-24 13:23

    Awk makes it quite simple:

    echo "1.2.14" | awk -F \. {'print $1,$2, $3'} will print out 1 2 14.

    flag -F specifies separator.

    If you wish to save one of the values:

    firstVariable=$(echo "1.2.14" | awk -F \. {'print $1'})

    0 讨论(0)
提交回复
热议问题