如何使用 bash 脚本匹配路径中的字符串/目录
我正在尝试创建一个 Makefile,它使用路径中的信息来创建相关的 rpm 名称。假设我有两个不同的可能路径:
PATH1 = /usr/local/home/jsmith/code/main
PATH2 = /usr/local/home/jsmith/code/dev/ver2
如果在路径中检测到“main”,我想检测“main”并将其附加到 rpm 名称。如果在路径中检测到“dev”,我想检测并将“ver2”附加到 rpm 名称。
我是 shell 脚本新手,真的不知道从哪里开始。我可以很容易地在Python之类的东西中做到这一点,但它是一个Makefile,所以我需要在shell中做到这一点。
路径中的“main”将是常量,但如果“main”不存在,则需要提取开发路径名称。以下是一些割草示例:
/usr/local/home/jsmith/code/main /usr/local/home/jsmith/code/dev/ver_usa /usr/local/home/jsmith/code/dev/ver_mexico /usr/local/home/jsmith/code/dev/ver3
如果“dev”存在,则需要提取“ver_usa”、“ver_mexico”、“ver3”等。需要提取的目录名称如下“开发”。
I'm trying to create a Makefile that uses information from the path to create a relevant rpm name. Suppose I have two different possible paths:
PATH1 = /usr/local/home/jsmith/code/main
PATH2 = /usr/local/home/jsmith/code/dev/ver2
If "main" is detected in the path, I want to detect and append "main" to the rpm name. If "dev" is detected in the path, I want to detect and append "ver2" to the rpm name.
I'm new to shell scripting and really don't have a good idea on where to start. I could easily do this in something like python, but its for a Makefile so I need to do it in shell.
"main" in the path would be constant, but if "main" doesn't exist, the dev path name would need to be extracted. Here's a few mow examples:
/usr/local/home/jsmith/code/main
/usr/local/home/jsmith/code/dev/ver_usa
/usr/local/home/jsmith/code/dev/ver_mexico
/usr/local/home/jsmith/code/dev/ver3
If "dev" existed, it would be needed to extract "ver_usa", "ver_mexico", "ver3", etc. The dir name needing to be extracted would exactly follow "dev".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
grep
并检查返回值?此外,您可以在 Makefile 中运行 python。
Use
grep
and check for return value?Also, you can run python in Makefile.
看起来路径的最后一个元素总是您想要的。
或者
It looks like the last element of the path is always the one you want.
or
像这样,假设“main”和“ver2”不是常数
something like this, assuming "main" and "ver2" are not constant