22

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
ephemient
  • 198,619
  • 38
  • 280
  • 391
Jaelebi
  • 5,879
  • 8
  • 32
  • 34

4 Answers4

35

Try adding a semicolon after cat testFile. For example:

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

alternatively:

read:
    test -r testFile && cat testFile
Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
jwa
  • 501
  • 4
  • 3
  • 2
    the alternate solution works but I have to use the if..then syntax. adding a semicolon does not seem to solve the issue. – Jaelebi Apr 19 '09 at 06:09
  • Weird. I tried it the first time with semicolon and it didnt work. th second time I ran it it worked.Thanks – Jaelebi Apr 19 '09 at 06:14
  • Just a note to self: I originally wrote plain multiline `bash` statements in the makefile, and had the same failure - and as this answer notes, the trick is to have the makefile recognize the shell command as a single line; hence I'd need _both_ semicolon `;` (to separate shell commands) _and_ backslash `\\` (to escape the subsequent newline) to get it to work... Cheers! – sdaau Aug 03 '11 at 13:33
  • See also https://stackoverflow.com/a/18554356/2336934 – dan Jul 17 '23 at 13:54
9

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

file:
    @if [ -e scripts/python.exe ] ; then \
    echo TRUE ; \
    fi
honkaboy
  • 91
  • 1
  • 1
5

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.

Evidlo
  • 173
  • 1
  • 8
2

I also met this problem.

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

HackNone
  • 504
  • 6
  • 12