如何在 Makefile 中的 shell 代码块内扩展通配符?

How to expand wildcard inside shell code block in a Makefile?

我得到了这个脚本,它读取我的 .gitignore 文件的分隔部分,并删除给定标记 # #:

之后的所有文件
# 
RAW_GITIGNORE_CONTENTS := $(shell while read -r line; do printf "$$line "; done < ".gitignore")
GITIGNORE_CONTENTS := $(shell echo "$(RAW_GITIGNORE_CONTENTS)" | sed -E $$'s/[^\#]+\# //g')

# 
DIRECTORIES_TO_CLEAN := $(shell /bin/find -not -path "./**.git**" -not -path "./pictures**" -type d)

clean:
#   
#   
    readarray -td' ' GARBAGE_DIRECTORIES <<<"$(DIRECTORIES_TO_CLEAN) "; \
    unset 'GARBAGE_DIRECTORIES[-1]'; \
    declare -p GARBAGE_DIRECTORIES; \
    readarray -td' ' GARBAGE_EXTENSIONS <<<"$(GITIGNORE_CONTENTS) "; \
    unset 'GARBAGE_EXTENSIONS[-1]'; \
    declare -p GARBAGE_EXTENSIONS; \
    for filename in "$${GARBAGE_DIRECTORIES[@]}"; \
    do \
        arraylength="$${#GARBAGE_EXTENSIONS[@]}"; \
        printf 'Cleaning %s extensions on %s\n' "$${arraylength}" "$$filename"; \
        for extension in "$${GARBAGE_EXTENSIONS[@]}"; \
        do \
            [[ ! -z "$$filename" ]] || continue; \
            [[ ! -z "$$extension" ]] || continue; \
            full_expression="$${filename}/$${extension}" ;\
            printf '%s\n' "$$full_expression"; \
            rm -v "$$full_expression"; \
        done; \
    done;

运行 它与以下 .gitignore 文件:

*.txt
*.var
# Comment #
*.aux

rm 命令没有扩展通配符,并一直告诉我 rm: cannot remove './*.aux': No such file or directory 不要从 ./ 目录中删除 *.aux 文件。


更新

在询问@Beta 的评论后,我将 Makefile 简化为:


GITIGNORE_CONTENTS := "*.aux" "*.lof"
DIRECTORIES_TO_CLEAN := "./setup/cache" "./setup/cache/chapters"

clean:
    readarray -td' ' GARBAGE_DIRECTORIES <<<"$(DIRECTORIES_TO_CLEAN) "; \
    unset 'GARBAGE_DIRECTORIES[-1]'; \
    declare -p GARBAGE_DIRECTORIES; \
    readarray -td' ' GARBAGE_EXTENSIONS <<<"$(GITIGNORE_CONTENTS) "; \
    unset 'GARBAGE_EXTENSIONS[-1]'; \
    declare -p GARBAGE_EXTENSIONS; \
    for filename in "$${GARBAGE_DIRECTORIES[@]}"; \
    do \
        arraylength="$${#GARBAGE_EXTENSIONS[@]}"; \
        printf 'Cleaning %s extensions on %s\n' "$${arraylength}" "$$filename"; \
        for extension in "$${GARBAGE_EXTENSIONS[@]}"; \
        do \
            [[ ! -z "$$filename" ]] || continue; \
            [[ ! -z "$$extension" ]] || continue; \
            full_expression="$${filename}/$${extension}" ;\
            printf '%s\n' "$$full_expression"; \
            rm -vf "$$full_expression"; \
        done; \
    done;

在 运行 之后输出的结果是:

$ make
readarray -td' ' GARBAGE_DIRECTORIES <<<""./setup/cache" "./setup/cache/chapters" "; \
unset 'GARBAGE_DIRECTORIES[-1]'; \
declare -p GARBAGE_DIRECTORIES; \
readarray -td' ' GARBAGE_EXTENSIONS <<<""*.aux" "*.lof" "; \
unset 'GARBAGE_EXTENSIONS[-1]'; \
declare -p GARBAGE_EXTENSIONS; \
for filename in "${GARBAGE_DIRECTORIES[@]}"; \
do \
        arraylength="${#GARBAGE_EXTENSIONS[@]}"; \
        printf 'Cleaning %s extensions on %s\n' "${arraylength}" "$filename"; \
        for extension in "${GARBAGE_EXTENSIONS[@]}"; \
        do \
                [[ ! -z "$filename" ]] || continue; \
                [[ ! -z "$extension" ]] || continue; \
                full_expression="${filename}/${extension}" ;\
                printf '%s\n' "$full_expression"; \
                rm -vf "$full_expression"; \
        done; \
done;
declare -a GARBAGE_DIRECTORIES=([0]="./setup/cache" [1]="./setup/cache/chapters")
declare -a GARBAGE_EXTENSIONS=([0]="*.aux" [1]="*.lof")
Cleaning 2 extensions on ./setup/cache
./setup/cache/*.aux
./setup/cache/*.lof
Cleaning 2 extensions on ./setup/cache/chapters
./setup/cache/chapters/*.aux
./setup/cache/chapters/*.lof

更简单

我简化为更简单的版本:

clean:
    rm -v "./setup/cache/*.aux";

运行这个,也不要删除文件:

$ make
rm -v "./setup/cache/*.aux";
rm: cannot remove './setup/cache/*.aux': No such file or directory
make: *** [Makefile:3: clean] Error 1

$ ls ./setup/cache/*.aux
./setup/cache/main.aux

在上面,在运行 ls之后,你可以看到文件仍然存在并且它就在那里。

我设法通过更改来修复它:

    rm -vf "$$full_expression"; \

收件人:

    rm -vf $${full_expression}; \