0

This code snippet is functional for a given list of dirs in 'dirlist.txt'

DIR_LIST = "dirlist.txt"

exclude_legacy:
    @echo "=== Checking dirs to exclude..."
    while read -r dirto_exclude; \
    do \
        if [[ "$${dirto_exclude}" = \#.* ]]; then \
            continue; \
        else \
            echo "Exclude this dir, $${dirto_exclude}"; \
            rm -rf $${dirto_exclude}; \
        fi \
    done < $(DIR_LIST)

Have two questions:

  1. Interested to know if there are better ways to achieve the same with the help of make's built-in functions.

  2. Running 'make -s -f Makefile' purges the desired dirs given in 'dirlist.txt'.However, the output from the echo statement (in the else block) prints lines with comments as well that were skipped in the condition given in the 'if' block. I expected the output to contain only those dirnames that are to be purged, which is shown as expected.

Why is this?

Thanks, Viji

vijayanmr
  • 21
  • 3

1 Answers1

0

Make is made to "make" files, not remove them, so don't get your hopes up about this sort of thing being pretty.

GNU Make (4.2) has the file function for reading files, see this answer:

DIR_LIST := $(file < dirlist.txt)

Looks like you have some comment-like syntax on leading '#'. In that case I'd try shelling out to a grep (not tested):

DIR_LIST := $(shell grep -v "#*" dirlist.txt)

Next we can use rm -rf for deleting all files in a single invocation:

exclude_legacy:
    @echo Removing directories "$(DIR_LIST)"
    rm -rf $(DIR_LIST)

If DIR_LIST turns up empty the rm command will fail. If you need that to work I'd give xargs a try.

Andreas
  • 5,086
  • 3
  • 16
  • 36
  • gmtt has a function for this: https://github.com/markpiffer/gmtt#call-clr-commentslines-with-hash-comments – Vroomfondel Jul 02 '20 at 12:05
  • @Andreas, Agree with your remarks on the use of make. Since posting my question, I have changed the way to get rid of the commented lines - instead of Makefile, this is now handled in an external shell script. – vijayanmr Jul 07 '20 at 15:13
  • @vijayanmr Yeah, making a (shell) script is often the best solution – Andreas Jul 09 '20 at 10:24