语法错误:文件结束意外(期待"fi")

Jae*_*ebi 13 bash makefile

我正在用bash编写一个makefile,我有一个目标,我试图找到一个文件是否存在,即使我认为语法是正确的,我仍然给我一个错误.

这是我试图运行的脚本

read: 
        if [ -e testFile] ; then \ 
        cat testFile\ 
        fi
Run Code Online (Sandbox Code Playgroud)

我正在使用制表符,这不是问题.

错误是(当我输入:"make read")

if [ -e testFile] ; then \
        cat testFile \
        fi
/bin/sh: Syntax error: end of file unexpected (expecting "fi")
make: *** [read] Error 2
Run Code Online (Sandbox Code Playgroud)

小智 21

尝试添加分号后cat testFile.例如:

read: 
    if [ -e testFile ] ; then  cat testFile ; fi
Run Code Online (Sandbox Code Playgroud)

或者:

read:
    test -r testFile && cat testFile
Run Code Online (Sandbox Code Playgroud)

  • 替代解决方案有效,但我必须使用if..then语法。添加分号似乎无法解决问题。 (2认同)

小智 7

我遇到了同样的问题.这应该这样做:

file:
    @if [ -e scripts/python.exe ] ; then \
    echo TRUE ; \
    fi
Run Code Online (Sandbox Code Playgroud)


Evi*_*dlo 5

从 GNU Make 3.82 开始,您可以.ONESHELL:在文件顶部添加内容,告诉 make 在单个 shell 中运行目标内的所有行。

.ONESHELL:
SHELL := /bin/bash

foobar:
    if true
    then
        echo hello there
    fi
Run Code Online (Sandbox Code Playgroud)

请参阅文档

在行前面添加@或添加.SILENT:下面的选项.ONESHELL:以抑制回显行。