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

时间:2019-04-06 02:12:00

标签: shell makefile wildcard expansion

我得到了这个脚本,该脚本读取了.gitignore文件的定界部分,并删除了给定标记# #之后的所有文件:

# https://stackoverflow.com/questions/55527923/how-to-stop-makefile-from-expanding-my-shell-output
RAW_GITIGNORE_CONTENTS := $(shell while read -r line; do printf "$$line "; done < ".gitignore")
GITIGNORE_CONTENTS := $(shell echo "$(RAW_GITIGNORE_CONTENTS)" | sed -E $$'s/[^\#]+\# //g')

# https://stackoverflow.com/questions/4210042/exclude-directory-from-find-command
DIRECTORIES_TO_CLEAN := $(shell /bin/find -not -path "./**.git**" -not -path "./pictures**" -type d)

clean:
#   https://stackoverflow.com/questions/10586153/split-string-into-an-array-in-bash
#   https://stackoverflow.com/questions/11289551/argument-list-too-long-error-for-rm-cp-mv-commands
    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后,您可以看到该文件仍然存在并且在那里。

1 个答案:

答案 0 :(得分:0)

我设法通过更改来解决它:

    rm -vf "$$full_expression"; \

收件人:

    rm -vf $${full_expression}; \