Bash Makefile中的条件语句是否有效

Bash Makefile中的条件语句是否有效,bash,shell,makefile,Bash,Shell,Makefile,我有下面的Makefile ~/w/i/craft-api git:develop ❯❯❯ cat Makefile ⏎ ✱ ◼ test: echo "TODO: write tests" generate-toc: if ! [ -x "$(command -v doctoc)&q

我有下面的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

每一行都被视为一个单独的命令,并传递给不同的shell实例。您可以使用
\
continuations来组合所有的行,这样就可以知道如何将它们作为一个长字符串传递给单个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
语句未关闭。缺少的
是问题所在。谢谢你的解释。