Extract version number from file in shell script

后端 未结 8 1439
醉话见心
醉话见心 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: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?

提交回复
热议问题