Eva*_*oll -2 shell make escape-characters
如何正确转义$()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
Run Code Online (Sandbox Code Playgroud)
当我尝试将其修改为
@if [ $( find myDir -type f -not -name '*.openapi.yaml' | wc -l ) != 0 ]; then \
Run Code Online (Sandbox Code Playgroud)
我明白了,
/bin/sh: line 0: [: !=: unary operator expected
Run Code Online (Sandbox Code Playgroud)
如果我试图逃避$,\$我会得到同样的错误,
你需要双$:
.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
Run Code Online (Sandbox Code Playgroud)
并运行一个示例:
.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
Run Code Online (Sandbox Code Playgroud)