48 lines
1.3 KiB
Bash
48 lines
1.3 KiB
Bash
#!/bin/bash
|
||
# Ver: 1.0 by Endial Fang (endial@126.com)
|
||
#
|
||
# 版本管理函数库
|
||
|
||
# 加载依赖项
|
||
source "/usr/local/lib/liblog.sh"
|
||
|
||
# 函数列表
|
||
|
||
# 获取语义化版本
|
||
# 参数:
|
||
# $1 - 版本字符串
|
||
# $2 - 版本部分,1 为 major,2 为 minor,3 为 patch
|
||
# 返回值:
|
||
# 版本数组,包含 major,minor 和 patch
|
||
get_sematic_version () {
|
||
local version="${1:?version is required}"
|
||
local section="${2:?section is required}"
|
||
local -a version_sections
|
||
|
||
# 用于解析版本的正则表达式:x.y.z
|
||
local -r regex='([0-9]+)(\.([0-9]+)(\.([0-9]+))?)?'
|
||
|
||
if [[ "$version" =~ $regex ]]; then
|
||
local i=1
|
||
local j=1
|
||
local n=${#BASH_REMATCH[*]}
|
||
|
||
while [[ $i -lt $n ]]; do
|
||
if [[ -n "${BASH_REMATCH[$i]}" ]] && [[ "${BASH_REMATCH[$i]:0:1}" != '.' ]]; then
|
||
version_sections[j]="${BASH_REMATCH[$i]}"
|
||
((j++))
|
||
fi
|
||
((i++))
|
||
done
|
||
|
||
local number_regex='^[0-9]+$'
|
||
if [[ "$section" =~ $number_regex ]] && (( section > 0 )) && (( section <= 3 )); then
|
||
echo "${version_sections[$section]}"
|
||
return
|
||
else
|
||
error "Section allowed values are: 1, 2, and 3"
|
||
return 1
|
||
fi
|
||
fi
|
||
}
|