Extract version number from file in shell script

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

    I prefer "cut" command for this kind of things

    major=`echo $version | cut -d. -f1`
    minor=`echo $version | cut -d. -f2`
    revision=`echo $version | cut -d. -f3`
    revision=`expr $revision + 1`
    
    echo "$major.$minor.$revision"
    

    I know this is not the shortest way, but for me it's simplest to understand and to read...

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

    Yet another shell way (showing there's always more than one way to bugger around with this stuff...):

    $ echo 1.2.3 | ( IFS=".$IFS" ; read a b c && echo $a.$b.$((c + 1)) )
    1.2.4
    

    So, we can do:

    $ x=1.2.3
    $ y=`echo $x | ( IFS=".$IFS" ; read a b c && echo $a.$b.$((c + 1)) )`
    $ echo $y
    1.2.4
    
    0 讨论(0)
提交回复
热议问题