我希望创建一个临时文件,并使用makefile将一些文本导入其中。
在bash中,我可以创建一个临时文件并将文本导入其中,如下所示:
temp_file=$(mktemp)
echo "text goes into file" > ${temp_file}
cat ${temp_file}
rm ${temp_file}运行时输出(如预期的那样):
text goes into file在makefile中使用相同的代码时,我得到以下输出:
makefile:
test:
temp_file=$(mktemp)
echo "text goes into file" > ${temp_file}
cat ${temp_file}
rm ${temp_file}$make test
echo "text goes into file" > /bin/sh: -c: line 1: syntax error near
unexpected token `newline' /bin/sh: -c: line 1: `echo "text goes into
file" > ' make: *** [makefile:18: test] Error 2你知道我在这里做错了什么吗?或者我是否遗漏了任何特殊的makefile语法规则?
发布于 2021-12-09 05:25:42
问题是,菜谱中的每一行都是在单独的shell调用中运行的,因此在一行中设置的shell变量在后续行中不可见(请参阅为什么当前目录在makefile中不改变?)。最重要的是,您需要加倍$标志,以便shell能够看到$。
但是,您可以使用Make变量而不是在这里使用shell变量:
TEMP_FILE := $(shell mktemp)
test:
echo "text goes into file" > $(TEMP_FILE)
cat $(TEMP_FILE)
rm $(TEMP_FILE)https://unix.stackexchange.com/questions/634954
复制相似问题