Shell 无法从Makefile调用函数

Shell 无法从Makefile调用函数,shell,makefile,gnu-make,Shell,Makefile,Gnu Make,我需要从make目标调用一个函数,这个函数会被多次调用 define generate_file if [ "${RQM_SETUP}" = "ci" ]; then echo "$1" > $(2).txt else echo "It is Not Setup"; fi endef all: $(call generate_file,John Doe,101)

我需要从make目标调用一个函数,这个函数会被多次调用

define generate_file
if [ "${RQM_SETUP}" = "ci" ]; then
    echo "$1" > $(2).txt
else
    echo "It is Not Setup";
fi
endef
all:
        $(call generate_file,John Doe,101)
        $(call generate_file,Peter Pan,102)
现在我陷入了这个错误:

bash-5.0# make
if [ "" = "ci" ]; then
/bin/sh: syntax error: unexpected end of file (expecting "fi")
make: *** [Makefile:10: all] Error 2

您的函数是多行的,它将尝试作为单独的shell调用执行。这将失败,因为任何一行本身的语法都不正确。您可以通过在一行中进行设置使其工作,即:

$ cat Makefile
define generate_file
if [ "${RQM_SETUP}" = "ci" ]; then \
    echo "$1" > $(2).txt; \
else \
    echo "It is Not Setup"; \
fi
endef
all:
        $(call generate_file,John Doe,101)
        $(call generate_file,Peter Pan,102)
输出:

$ make
if [ "" = "ci" ]; then echo "John Doe" > 101.txt; else echo "It is Not Setup"; fi
It is Not Setup
if [ "" = "ci" ]; then echo "Peter Pan" > 102.txt; else echo "It is Not Setup"; fi
It is Not Setup

嘿@raspy感谢你澄清了这一点,现在上面的代码可以工作了,所以我尝试用我想做的实际事情来实现它,在RQM_SETUP=ci通过时,它可以按预期工作,但在其他部分失败,@MohammedAli请不要这样做。拉斯比回答了你原来的问题。如果你因此遇到了一个新问题,那应该是一个新问题。