makefile - Makefile 检查文件夹和/或文件是否存在
问题描述
我有以下 Makefile:
build: clean
${GOPATH}/bin/dep ensure
env GOOS=linux go build -o ./bin/status ./lib/status/main.go
elm-app build
init:
${GOPATH}/bin/dep init -v
test:
env GOOS=linux go test -v ./lib/status
strip:
strip ./bin/status
clean:
if [ -f ./bin/status ]; then
rm -f ./bin/status
fi
但我明白了
if [ -f ./bin/status ]; then
/bin/sh: 1: Syntax error: end of file unexpected
Makefile:16: recipe for target 'clean' failed
make: *** [clean] Error 2
我错过了什么?
非常感谢任何建议
解决方案
makefile 的每一行都在一个单独的 shell 中运行。这意味着您的规则:
clean:
if [ -f ./bin/status ]; then
rm -f ./bin/status
fi
实际运行以下命令:
/bin/sh -c "if [ -f ./bin/status ]; then"
/bin/sh -c "rm -f ./bin/status"
/bin/sh -c "fi"
您可以看到为什么会收到此消息。为确保将所有行发送到单个 shell,您需要使用反斜杠来继续这些行,如下所示:
clean:
if [ -f ./bin/status ]; then \
rm -f ./bin/status; \
fi
请注意,这意味着您还需要在rm
命令后使用分号,以便将其与结尾分开fi
。
现在你得到一个这样的 shell 调用:
/bin/sh -c "if [ -f ./bin/status ]; then \
rm -f ./bin/status; \
fi"
推荐阅读
- c - 如何编辑读取文件中集合的平均数量并将它们写入新文件的程序?
- amazon-web-services - 如何在 dynamodb 中为数据量大的阅读理解测验应用程序设计模式?
- django - django order_by 任一列(日期以最近为准)
- c# - WPF 使用 IValueConverter 转换 TextBox 文本
- python - 查找 panda 数据框中的共同元素
- java - CWWMQ0062E: 从 IBM 队列连接工厂启用 SSL 时接收错误
- android - 如何在 Android 中移除按钮点击效果?
- java - 使用 .hasNext() 循环输入对
- pdf - CakePHP 和 FPDF:在浏览器中查看的标题
- python - 我如何理解鼠标按下的时间?