2009-04-19 126 views
10

我在bash中编写一个makefile,我有一个目标,在其中尝试查找文件是否存在,即使我认为语法正确,我仍然给我一个错误。语法错误:文件意外结束(期待“fi”)

这里是我想

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

我使用的标签,这样是不是有问题来运行该脚本。

错误的是(当我键入: “让阅读”)

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

回答

13

尝试cat testFile后添加一个分号。例如:

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

或者:

read: 
    test -r testFile && cat testFile 
+1

备用解决方案的工作原理,但我必须使用if..then语法。添加分号似乎不能解决问题。 – Jaelebi 2009-04-19 06:09:25

+0

奇怪。我第一次用分号尝试它,它没有工作。第二次运行它,它工作。谢谢 – Jaelebi 2009-04-19 06:14:08

2

我也遇到了这个问题。

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

5

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

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

由于GNU制作3.82,您可以添加.ONESHELL:到文件的顶部告诉使得运行目标中的所有线路在一个单一的外壳。

.ONESHELL: 
SHELL := /bin/bash 

foobar: 
    if true 
    then 
     echo hello there 
    fi 

查看documentation

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

相关问题