使用 Bash 从包名称中去除版本
我正在尝试仅使用 Bash 从包名称中删除版本。我有一个解决方案,但我认为这不是最好的解决方案,所以我想知道是否有更好的方法。我所说的更好是指更干净、更容易理解。
假设我有字符串“my-program-1.0”,而我只想要“my-program”。我当前的解决方案是:
#!/bin/bash
PROGRAM_FULL="my-program-1.0"
INDEX_OF_LAST_CHARACTER=`awk '{print match($0, "[A-Za-z0-9]-[0-9]")} <<< $PROGRAM_FULL`
PROGRAM_NAME=`cut -c -$INDEX_OF_LAST_CHARACTER <<< $PROGRAM_FULL`
实际上,“包名称”语法是一个 RPM 文件名(如果有的话)。
谢谢!
I'm trying to strip the version out of a package name using only Bash. I have one solution but I don't think that's the best one available, so I'd like to know if there's a better way to do it. by better I mean cleaner, easier to understand.
suppose I have the string "my-program-1.0" and I want only "my-program". my current solution is:
#!/bin/bash
PROGRAM_FULL="my-program-1.0"
INDEX_OF_LAST_CHARACTER=`awk '{print match($0, "[A-Za-z0-9]-[0-9]")} <<< $PROGRAM_FULL`
PROGRAM_NAME=`cut -c -$INDEX_OF_LAST_CHARACTER <<< $PROGRAM_FULL`
actually, the "package name" syntax is an RPM file name, if it matters.
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
非常适合 sed:
第二个匹配确保版本号是由点分隔的数字序列(例如 X、XX、XXX,...)。
编辑:因此,基于版本号概念定义不明确的事实,到处都有评论。您必须为您期望的输入编写正则表达式。希望您不会遇到像“program-name-1.2.3-a”那样可怕的事情。不过,如果没有OP的任何额外要求,我认为这里的所有答案都足够好了。
Pretty well-suited to sed:
The second match ensures that the version number is a sequence of numbers separated by dots (e.g. X, X.X, X.X.X, ...).
Edit: So there are comments all over based on the fact that the notion of version number isn't very well-defined. You'll have to write a regex for the input you expect. Hopefully you won't have anything as awful as "program-name-1.2.3-a". Absent any additional request from the OP though, I think all the answers here are good enough.
Bash:
生成“my-program”
或
生成“alsa-lib”
Bash:
Produces "my-program"
Or
Produces "alsa-lib"
怎么样:
How about:
当传递完整的包名称来代替
pkg_name
时,它会删除版本并仅提供包名称。When the full package name is passed in place of
pkg_name
, it removes the version and gives only the package name.