1

linux~更新版本号的shell

 2 years ago
source link: https://www.cnblogs.com/lori/p/16068260.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

linux~更新版本号的shell

一个shell,定义一个函数,版本号做为参数,来更新这个版本号,如1.0,更新结果为1.1

increment_version ()
{
  declare -a part=( ${1//\./ } )
  declare    new
  declare -i carry=1

  for (( CNTR=${#part[@]}-1; CNTR>=0; CNTR-=1 )); do
    len=${#part[CNTR]}
    new=$((part[CNTR]+carry))
    [ ${#new} -gt $len ] && carry=1 || carry=0
    [ $CNTR -gt 0 ] && part[CNTR]=${new: -len} || part[CNTR]=${new}
  done
  new="${part[*]}"
  echo -e "${new// /.}"
} 
version='1.1.9'

increment_version $version
# EXAMPLE   ------------->   # RESULT
increment_version.sh 1.9         # 2.0
increment_version.sh 1.0.0      # 1.0.1
increment_version.sh 1.1        # 1.2
increment_version.sh 1.1.00     # 1.1.01

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK