user11409749
user11409749

Reputation:

How to use shell command in GNU Make to echo string

I have the following lines in my makefile:

.PHONY : clean
clean:
    @echo "Running Clean"
    $(shell if [ -e exe ]; then rm exe; else echo "no files"; fi)

When I run:

make clean

I get the following output on the shell

Running Clean
no files
make: no: Command not found
Makefile:22: recipe for target 'clean' failed
make: *** [clean] Error 127

Any suggestions?

Upvotes: 0

Views: 1087

Answers (2)

HardcoreHenry
HardcoreHenry

Reputation: 6387

The problem is the use of $(shell ...). What you want is:

.PHONY : clean
clean:
    @echo "Running Clean"
    @if [ -e exe ]; then rm exe; else echo "no files"; fi

As far as an explanation of what's going wrong -- when you first run the clean target, make will expand all make variables and functions in the recipes before it starts running them -- because $(shell ...) only has one $, this is considered a make function. Make runs the command, which outputs no files to stdout, and replaces the call with that string, and then starts executing the recipes... So now make sees the following:

clean:
    @echo "Running Clean"
    no files

When it tries to run no files, due to the lack of a @, it echos the line to the screen, and then passes the command to the shell. Because the shell doesn't recognize the keyword no it outputs the error you're seeing. Make itself then fails because the shell returned an error.

Upvotes: 2

user11409749
user11409749

Reputation:

Hey all I'm the same guy who asked this question but I found an answer right after I posted this, I think I'll leave this up (unless this is against stackoverflow etiquette) in case someone else has the same problems. My solution was echoing the string to stdout.

$(shell if [ -e exe ]; then rm exe; else echo "no files" >&2; fi)

Upvotes: 1

Related Questions