从 shell 脚本中的文件中提取版本号 [英] Extract version number from file in shell script

查看:117
本文介绍了从 shell 脚本中的文件中提取版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 bash 脚本来增加

I'm trying to write a bash script that increments the version number which is given in

{major}.{minor}.{revision}

例如.

1.2.13

是否有一种好方法可以使用 sed 或 awk 之类的东西轻松提取这 3 个数字,以便我可以增加 {revision} 数字并输出完整的版本号字符串.

Is there a good way to easily extract those 3 numbers using something like sed or awk such that I could increment the {revision} number and output the full version number string.

推荐答案

$ v=1.2.13
$ echo "${v%.*}.$((${v##*.}+1))"
1.2.14

<小时>

$ v=11.1.2.3.0
$ echo "${v%.*}.$((${v##*.}+1))"
11.1.2.3.1

<小时>

这是它的工作原理:


Here is how it works:

字符串分为两部分.

  • 第一个包含除最后一个点和下一个字符之外的所有内容:${v%.*}
  • 第二个包含除最后一个点之前的所有字符之外的所有内容:${v##*.}

第一部分按原样打印,然后是一个普通点,最后一部分使用 shell 算术扩展递增:$((x+1))

The first part is printed as is, followed by a plain dot and the last part incremented using shell arithmetic expansion: $((x+1))

这篇关于从 shell 脚本中的文件中提取版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆