How to increment version number in a shell script?

前端 未结 11 2083
执念已碎
执念已碎 2020-11-29 23:47

The following simple version control script is meant to find the last version number of a given file, increment it, run a given command with the newly created file (e.g., ed

11条回答
  •  一个人的身影
    2020-11-30 00:26

    Determining a version number for a software project is based on its relative change / functionality / development stage / revision. Consequent increments to the version and revision numbering is ideally a process that should be done by a human. However, not to second-guess your motivation for writing this script, here is my suggestion.

    Include some logic in your script that will do exactly what you describe in your requirement

    "...if the last section after dot has two digits, inc until 99; if only 1, then inc until 9 ... "

    Assuming the third position is the development stage number $dNum and the fourth (last) position is the revision number $rNum:

    if  [ $(expr length $rNum) = "2" ] ; then 
        if [ $rNum -lt 99 ]; then 
            rNum=$(($rNum + 1))
        else rNum=0
             dNum=$(($dNum + 1)) #some additional logic for $dNum > 9 also needed
        fi
    elif [ $(expr length $dNum) = "1" ] ; then
        ...
        ...
    fi
    

    Perhaps a function will allow the most succinct way of handling all positions (majNum.minNum.dNum.rNum).

    You will have to separate the project name and version number components of your filename in your script and then construct the version number with all its positions, and finally reconstruct the filename with something like

    new_version="$majNum.minNum.$dNum.$rNum"
    new_version_file="$old_file.$new_version"
    

    Hope that helps and check this SO discussion as well as this wikipedia entry if you want to know more about versioning conventions.

提交回复
热议问题