Extract version number from file in shell script

后端 未结 8 1445
醉话见心
醉话见心 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))

提交回复
热议问题