如何$()
在 Makefile 中正确转义 shell 扩展语法?
.PHONY: test
test:
@if [ `find myDir -type f -not -name '*.openapi.yaml' | wc -l ` != 0 ]; then \
echo "All files must end with '.openapi.yaml'"; \
find myDir -type f -not -name '*.openapi.yaml'; \
exit 42; \
fi
当我尝试将其修改为
@if [ $( find myDir -type f -not -name '*.openapi.yaml' | wc -l ) != 0 ]; then \
我明白了,
/bin/sh: line 0: [: !=: unary operator expected
如果我尝试逃避$
with\$
我会得到同样的错误,
答案1
你需要双$
:
.PHONY: test test1 test2
test: test1 test2
test1:
@echo test1; if [ $$(echo 0) == "0" ]; then echo true; else echo false; fi
test2:
@echo test2; if [ $$(echo 0) == "1" ]; then echo true; else echo false; fi
并运行一个示例:
$ make
test1
true
test2
false