Reputation: 41955
I am trying to execute a command in a conditional in a makefile.
I got it working in a shell:
if [ -z "$(ls -A mydir)" ]; then \
echo "empty dir"; \
else \
echo "non-empty dir"; \
fi
but if I try it in a makefile, "$(ls -A mydir)"
expands to nothing whatever if asdf
is empty or not:
all:
if [ -z "$(ls -A mydir)" ]; then \
echo "empty dir"; \
else \
echo "non-empty dir"; \
fi
The ls
command does not expand as I expect:
$ mkdir mydir
$ make
if [ -z "" ]; then \
echo "empty dir"; \
else \
echo "non-empty dir"; \
fi
empty dir
$ touch mydir/myfile
$ make
if [ -z "" ]; then \
echo "empty dir"; \
else \
echo "non-empty dir"; \
fi
empty dir
$ ls -A mydir
myfile
How do I make the command work inside the conditional?
Upvotes: 0
Views: 2489
Reputation:
I have little experience in writing makefiles. However I think you must use two dollar signs in your recipe:
all:
if [ -z "$$(ls -A mydir)" ]; then \
https://www.gnu.org/software/make/manual/make.html#Variables-in-Recipes:
if you want a dollar sign to appear in your recipe, you must double it (‘$$’).
This is an example of output after I changed your makefile and added $$(ls -A mydir)
:
$ ls mydir/
1
$ make
if [ -z "$(ls -A mydir)" ]; then \
echo "empty dir"; \
else \
echo "non-empty dir"; \
fi
non-empty dir
$ rm mydir/1
$ make
if [ -z "$(ls -A mydir)" ]; then \
echo "empty dir"; \
else \
echo "non-empty dir"; \
fi
empty dir
Upvotes: 5