如何使用 bash 脚本匹配路径中的字符串/目录

发布于 2024-08-24 04:04:42 字数 646 浏览 2 评论 0原文

我正在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

庆幸我还是我 2024-08-31 04:04:43

使用 grep 并检查返回值?

此外,您可以在 Makefile 中运行 python。

Use grep and check for return value?

Also, you can run python in Makefile.

烟雨凡馨 2024-08-31 04:04:43

看起来路径的最后一个元素总是您想要的。

rpmname=$rpmname+${pathname##*/}

或者

rpmname=$rpmname+$(basename pathname)

It looks like the last element of the path is always the one you want.

rpmname=$rpmname+${pathname##*/}

or

rpmname=$rpmname+$(basename pathname)
败给现实 2024-08-31 04:04:42

像这样,假设“main”和“ver2”不是常数

some_rpm_name="some rpm"
PATH=/usr/local/home/jsmith/code/main
#PATH2=/usr/local/home/jsmith/code/dev/ver2
s=${PATH##*/}
case "$s" in
  *main ) RPM_NAME="${some_rpm_name}_main";;
  *ver2) RPM_NAME="${some_rpm_name}_ver2";;
esac
echo $RPM_NAME

something like this, assuming "main" and "ver2" are not constant

some_rpm_name="some rpm"
PATH=/usr/local/home/jsmith/code/main
#PATH2=/usr/local/home/jsmith/code/dev/ver2
s=${PATH##*/}
case "$s" in
  *main ) RPM_NAME="${some_rpm_name}_main";;
  *ver2) RPM_NAME="${some_rpm_name}_ver2";;
esac
echo $RPM_NAME
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文