bash 语法错误:意外的文件结尾(期待“fi”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/764908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:07:20  来源:igfitidea点击:

Syntax error : end of file unexpected (expecting "fi")

bashmakefile

提问by Jaelebi

I am writing a makefile in bash and I have a target in which I try to find if a file exists and even though I think the syntax is correct, i still gives me an error.

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

Here is the script that I am trying to run

这是我试图运行的脚本

read: 
        if [ -e testFile] ; then \ 
        cat testFile\ 
        fi

I am using tabs so that is not a problem.

我正在使用标签,所以这不是问题。

The error is (when I type in: "make read")

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

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

回答by jwa

Try adding a semicolon after cat testFile. For example:

尝试在 之后添加分号cat testFile。例如:

read: 
    if [ -e testFile ] ; then  cat testFile ; fi

alternatively:

或者:

read:
    test -r testFile && cat testFile

回答by honkaboy

I ran into the same issue. This should do it:

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

file:
    @if [ -e scripts/python.exe ] ; then \
    echo TRUE ; \
    fi

回答by Evidlo

Since GNU Make 3.82, you can add .ONESHELL:to the top of the file to tell make to run all the lines within a target in a single shell.

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

.ONESHELL:
SHELL := /bin/bash

foobar:
    if true
    then
        echo hello there
    fi

See the documentation.

请参阅文档

Prepend lines with @or add the .SILENT:option beneath .ONESHELL:to suppress echoing lines.

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

回答by HackNone

I also met this problem.

我也遇到了这个问题。

And the reason is that I added some comments after the "\".

原因是我在“\”之后添加了一些注释。