Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

makefile

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.

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")

if [ -e testFile] ; then \
        cat testFile \
        fi
/bin/sh: Syntax error: end of file unexpected (expecting "fi")
make: *** [read] Error 2
like image 858
Jaelebi Avatar asked Apr 19 '09 05:04

Jaelebi


People also ask

What does Unexpected end of file mean in Linux?

An Unexpected end of file error in a Bash script usually occurs when you there is a mismatched structure somewhere in the script. If you forget to close your quotes, or you forget to terminate an if statement, while loop, etc, then you will run into the error when you try to execute your Bash script.

How do you end a file in bash?

One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.


3 Answers

Try adding a semicolon after cat testFile. For example:

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

alternatively:

read:
    test -r testFile && cat testFile
like image 179
jwa Avatar answered Oct 27 '22 01:10

jwa


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

file:
    @if [ -e scripts/python.exe ] ; then \
    echo TRUE ; \
    fi
like image 32
honkaboy Avatar answered Oct 27 '22 00:10

honkaboy


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.

.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.

like image 31
Evidlo Avatar answered Oct 27 '22 00:10

Evidlo