编辑: :要仅更改MINOR版本,请尝试以下操作。
echo "$version" | awk 'BEGIN{FS=OFS="."} {$3+=1} 1'
说明: 在上述代码中添加说明。
echo "$version" | ##using echo to print variable version value here and passing it to awk program.
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of this awk program here.
FS=OFS="." ##Setting FS and OFS as dot(.) here for all lines of Input_file.
}
{
$3+=1 ##For every line of Input_file adding 1 to 3rd field and saving output to $3 itself.
}
1 ##Mentioning 1 will print edited/non-edited lines here.
'
请问您可以尝试按照上一(次要)版本来更改中间版本,OP确认不需要,但我乍一看就知道了这一要求。
echo "$version" | awk 'BEGIN{FS=OFS="."} {$3+=1;if($3>9){$2+=1;$3=0}} 1'
,
纯Bash解决方案:
#!/usr/bin/env bash
version='1.27.9'
# Read Semver fields
IFS=. read -r major minor patch <<<"$version"
# Increment patch version
# same as patch="$((patch + 1))"
((patch++))
# Re-assemble version string from Semver fields
printf -v version '%d.%d.%d' "$major" "$minor" "$((patch))"
-
IFS=.
;在.
命令的本地范围内将字段分隔符设置为read
。
-
read -r major minor patch <<<"$version"
:从"$version
此处的字符串中读取Semver字段。
-
((patch++))
:使用Bash的独立算法递增$patch
版本。 (可以替换为patch="$((patch + 1))"
)
-
printf -v version '%d.%d.%d' "$major" "$minor" "$((patch))"
:使用Bash的$version
(输出到变量)功能,将printf -v variable
字符串格式化为具有更新的字段。
相当于@RavinderSingh13 's version的简写单行:
IFS=. read -r a b c<<<"$version";echo "$a.$b.$((c+1))"
本文链接:https://www.f2er.com/2881499.html