如何在bash中拆分目录名称以在路径中的所有目录中进行递归调用

发布于 2024-12-26 07:30:14 字数 556 浏览 0 评论 0原文

抱歉标题令人困惑,我很难描述这个问题。

我有两个可用的值,第一个是常量,第二个可以是任意数量的文件夹级别

BASE = application/classes
FOLDERS = types/ads

基于这些值,我需要运行如下命令(按顺序):

verifyDir application/classes
verifyDir application/classes/types
verifyDir application/classes/types/ads

我也可以传递所有目录到 verifyDir 如果这样更容易的话

verifyDir application/classes application/classes/types application/classes/types/ads

这是在 makefile 中,因此使用 gmake 的函数是可以接受的。

请不要回答重组我正在做的事情的建议。解释我为什么需要这个需要很长时间。

Sorry about the confusing title, I had a hard time describing the problem.

I have two values available to me, the first one is a constant and the second one could be any number of folder levels

BASE = application/classes
FOLDERS = types/ads

Based on those, I need to run commands like the following (in that order):

verifyDir application/classes
verifyDir application/classes/types
verifyDir application/classes/types/ads

I could also just pass all the directories to verifyDir if that's easier

verifyDir application/classes application/classes/types application/classes/types/ads

This is within a makefile, so using gmake's functions is acceptable.

Please don't answer with a suggestion to restructure what I am doing. It would take way too long to explain why I need this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

江挽川 2025-01-02 07:30:14

我不是 gmake 大师,所以我只能提出一种使用 shell 实现这一目标的方法,也许它可以帮助您在 gnu make 中实现这一目标...

BASE=application/classes
FOLDERS=types/ads

verifyDir $BASE
NEWBASE=$BASE
for c in `echo $FOLDERS | tr "/" " "`; do
    NEWBASE="$NEWBASE/$c"
    verifyDir $NEWBASE
done

I'm not a gmake master, so I can just propose a way how to achieve that using shell, may be it helps you to achieve that in gnu make...

BASE=application/classes
FOLDERS=types/ads

verifyDir $BASE
NEWBASE=$BASE
for c in `echo $FOLDERS | tr "/" " "`; do
    NEWBASE="$NEWBASE/$c"
    verifyDir $NEWBASE
done
半边脸i 2025-01-02 07:30:14

安德鲁的建议很棒。工作中有人提出了另一种解决方案,因此我将其放在这里以供参考,因为它使用了不同的技术(IFS,它更改了默认的单词分隔符)

BASE="some/base/dir" 
TAIL="fold1/fold2/fold3" 
ALLPATHS="$BASE"
IFS=/
for X in $TAIL; do
    ALLPATHS="$ALLPATHS $ALLPATHS/$X";
done; 
unset IFS; 
verifyDir $ALLPATHS;

Andrew's suggestion is great. Somebody at work came up up with another solution so I'll put it here for reference since it uses a different technique (IFS, which changes the default word separator)

BASE="some/base/dir" 
TAIL="fold1/fold2/fold3" 
ALLPATHS="$BASE"
IFS=/
for X in $TAIL; do
    ALLPATHS="$ALLPATHS $ALLPATHS/$X";
done; 
unset IFS; 
verifyDir $ALLPATHS;
浪推晚风 2025-01-02 07:30:14

另一种解决方案,使用 GNU make 函数

BASE = application/classes
FOLDERS = types/ads/more/hierarchy/here

FOLDERS_SPLIT := $(subst /, ,$(FOLDERS))
FOLDERS_ITER :=

FOLDERS_TREE := $(foreach FOLDER, $(FOLDERS_SPLIT), $(eval FOLDERS_ITER := $(FOLDERS_ITER)/$(FOLDER)) $(BASE)$(FOLDERS_ITER))

all:
    @echo FOLDERS_TREE $(FOLDERS_TREE)

Another solution, using GNU make functions

BASE = application/classes
FOLDERS = types/ads/more/hierarchy/here

FOLDERS_SPLIT := $(subst /, ,$(FOLDERS))
FOLDERS_ITER :=

FOLDERS_TREE := $(foreach FOLDER, $(FOLDERS_SPLIT), $(eval FOLDERS_ITER := $(FOLDERS_ITER)/$(FOLDER)) $(BASE)$(FOLDERS_ITER))

all:
    @echo FOLDERS_TREE $(FOLDERS_TREE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文