简体   繁体   English

foreach 目录,检查条件然后运行管道 bash 命令

[英]foreach directory, check condition then run piped bash commands

Hello people of the inte.net,互联网的人们,您好,

Firstly I must apologize as I am relatively new to makefiles, so I may be doing this in a very unorthodox way.首先,我必须道歉,因为我对 makefile 比较陌生,所以我可能会以一种非常不正统的方式来做这件事。

I am trying to create a makefile that will我正在尝试创建一个 makefile
-loop through each subdirectory of a specified directory -循环遍历指定目录的每个子目录
-check the directory is the specified directory or one of the subdirectories -检查目录是指定目录还是其中一个子目录
-run the directory's through tree and then generate an image of the tree using convert with the filepath removed. - 通过树运行目录,然后使用删除文件路径的转换生成树的图像。

At this point, I'm getting absolutely lost and have no idea where these issues are coming from.在这一点上,我完全迷路了,不知道这些问题是从哪里来的。 The whole idea is to make this code project agnostic.整个想法是让这个代码项目不可知。 I have a hard coded version that works fine with all of the directories and subdirectories specified manually.我有一个硬编码版本,可以很好地处理手动指定的所有目录和子目录。

I have tried two methods so far.到目前为止,我已经尝试了两种方法。

Method 1方法一

PROJECT_DIR = ../hub/hub/  
SUB_DIRECTORIES = $(shell find $(PROJECT_DIR) -maxdepth 1 -type d -exec echo {} \;)  
SUB_DIRECTORIES_FORMATTED = $(SUB_DIRECTORIES) | sed 's|..\/hub\/hub\/||g' | sed 's/\s\+/|/g'  

all:  
    @echo $(SUB_DIRECTORIES)  
    for $(directory) in $(SUB_DIRECTORIES); do \  
        if test $$directory = $(PROJECT_DIR); then \  
            echo "Generating root.png" && tree -P '*.py' -I '__pycache__|.c' $(directory) | convert -size 364x422 label:@- ./source/root.png; \
        else \
            "Generating $(subst $(PROJECT_DIR),,$(directory)) png" && tree -P '*.py' -I '__pycache__|.c' $(directory) | convert -size 364x422 label:@- ./source/$(subst $(PROJECT_DIR),,$(directory)).png; \
        fi \
    done

With this method, the subst isn't working thus resulting in these errors: Method 1 errors使用这种方法,subst 不工作从而导致这些错误:方法 1 错误

Method 2方法二

PROJECT_DIR = ../hub/hub/
SUB_DIRECTORIES =: $(shell find $(PROJECT_DIR) -maxdepth 1 -type d -exec echo {} \;)
SUB_DIRECTORIES_FORMATTED = $(SUB_DIRECTORIES) | sed 's|..\/hub\/hub\/||g' | sed 's/\s\+/|/g'

all:  
    @$(foreach file, $(wildcard $(SUB_DIRECTORIES)/), \  
        $(if $(filter $(file), $(PROJECT_DIR)), \  
            $(shell tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 364x422 label:@- ./source/root.png;), \  
            $(shell tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 364x422 label:@- ./source/$(subst $(PROJECT_DIR),,$(file)).png;) \  
        ) \  
    );

This method causes another error: method 2 errors此方法导致另一个错误:方法2错误

thank you for your comments.谢谢您的意见。

The issue was coming from the added "" after $(SUB_DIRECTORIES) and the semi-colon at the end.问题来自 $(SUB_DIRECTORIES) 之后添加的“”和末尾的分号。 It's relieving to know that I was not far off from the solution.知道我离解决方案不远了,这让我松了一口气。 I was able to get it to work with Method 2 using the following code.我能够使用以下代码让它与方法 2一起工作。 : :

PROJECT_DIR = ../hub/hub/
SUB_DIRECTORIES = $(shell find $(PROJECT_DIR) -maxdepth 1 -type d -exec echo {} \;)
SUB_DIRECTORIES_FORMATTED = $(SUB_DIRECTORIES) | sed 's|..\/hub\/hub\/||g' | sed 's/\s\+/|/g'

all:
    $(foreach file, $(wildcard $(SUB_DIRECTORIES)), \
        $(if $(filter $(file), $(PROJECT_DIR)), \
            $(shell tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 1280x1024 label:@- ./source/root.png), \
            $(shell tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 1280x1024 label:@- ./source/$(subst $(PROJECT_DIR),,$(file)).png) \
        ) \
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM