Extract version number from file in shell script

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

提交回复
热议问题