2009-04-19 132 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:選項以抑制回顯行。

相關問題