在Makefile中,条件语句是否是有效的语法?

6
我有以下的Makefile文件:
~/w/i/craft-api git:develop ❯❯❯ cat Makefile                                                                                     ⏎ ✱ ◼
test:
    echo "TODO: write tests"
generate-toc:
    if ! [ -x "$(command -v doctoc)" ]; then
        echo "Missing doctoc. Run 'npm install doctoc -g' first"
    else
        doctoc ./README.md
    fi

我遇到了这个错误。
~/w/i/craft-api git:develop ❯❯❯ make generate-toc                                                                                  ✱ ◼
if ! [ -x "" ]; then
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [generate-toc] Error 2

我的Makefile语法/用法有什么错误?

编辑1

添加行继续反斜杠似乎不能解决问题:

~/w/i/craft-api git:develop ❯❯❯ cat Makefile                                                                                     ⏎ ✱ ◼
test:
    echo "TODO: write tests"
generate-toc:
    if ! [ -x "$(command -v doctoc)" ]; then \
      echo "Missing doctoc. Run 'npm install doctoc -g' first" \
    else \
        doctoc ./README.md \
    fi
~/w/i/craft-api git:develop ❯❯❯ make generate-toc                                                                                  ✱ ◼
if ! [ -x "" ]; then \
      echo "Missing doctoc. Run 'npm install doctoc -g' first" \
    else \
        doctoc ./README.md \
    fi
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [generate-toc] Error 2
1个回答

8

每一行都被视为一个单独的命令,并传递给不同的shell实例。您可以使用\继续将所有行组合起来,以便make知道将它们作为一个长字符串传递给单个shell。这将删除换行符,因此您还需要在每个命令的末尾添加;

if ! [ -x "$$(command -v doctoc)" ]; then \
    echo "Missing doctoc. Run 'npm install doctoc -g' first"; \
else \
    doctoc ./README.md; \
fi

您还需要转义 $,否则 make 命令会将其解释为 shell 参数。


你在 echo 行末缺少必要的 ;。没有它,else(以及其他所有内容)被视为 echo 参数的一部分,而 if 语句未被关闭。 - Etan Reisner
缺少分号是问题所在。谢谢你的解释。 - Casey Flynn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接