Linux shell脚本实现x.x.x格式递增版本号,每次运行脚本自动将最后一位版本号+1,每次更新后将最新的版本号保存到当前txt文件内容,以保持连续递增版本号。
脚本文件目录
root@ubuntu01:/scripts/version# tree
.
├── increment_version.sh
└── version.txt
increment_version.sh文件内容
#!/bin/bash
# 只增加最后一位,实现版本号递增
# 当前版本号实例 1.0.11
# 当前版本号存储在本地文件txt,这里是当前目录txt文件名字
file="version.txt"
# 从当前txt文件读取版本号
current_version=$(cat $file)
# 使用正则表达式匹配版本号的每一部分
major=$(echo $current_version | cut -d '.' -f 1)
minor=$(echo $current_version | cut -d '.' -f 2)
patch=$(echo $current_version | cut -d '.' -f 3)
# 将补丁号加1
let patch++
# 输出新的版本号
new_version="${major}.${minor}.${patch}"
# 打印出当前新的版本号
echo "new_version="+$new_version
# 将新的版本号更新到本地文件
echo $new_version > $file
version.txt文件内容
1.0.11
测试执行结果
root@ubuntu01:/scripts#
root@ubuntu01:/scripts# cat version.txt
1.5.13
root@ubuntu01:/scripts# ./increment_version.sh
new_version=+1.5.14
root@ubuntu01:/scripts# cat version.txt
1.5.14
root@ubuntu01:/scripts#
其他格式和要求,请大家根据自己的实际情况调整脚本。