我试图在makefile中的条件中执行命令.
我让它在shell中工作:
if [ -z "$(ls -A mydir)" ]; then \
echo "empty dir"; \
else \
echo "non-empty dir"; \
fi
Run Code Online (Sandbox Code Playgroud)
但是如果我在makefile中尝试它,那么"$(ls -A mydir)"
无论是否asdf
为空都会扩展为空:
all:
if [ -z "$(ls -A mydir)" ]; then \
echo "empty dir"; \
else \
echo "non-empty dir"; \
fi
Run Code Online (Sandbox Code Playgroud)
该ls
命令没有像我期望的那样扩展:
$ 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
Run Code Online (Sandbox Code Playgroud)
如何使命令在条件内工作?
我在编写makefile时没什么经验.但是我想你必须在食谱中使用两个美元符号:
all:
if [ -z "$$(ls -A mydir)" ]; then \
Run Code Online (Sandbox Code Playgroud)
https://www.gnu.org/software/make/manual/make.html#Variables-in-Recipes:
如果您希望在食谱中出现美元符号,则必须将其加倍('$$').
这是我更改你的makefile并添加后输出的一个例子$$(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
Run Code Online (Sandbox Code Playgroud)