0

I am passing an argument to makefile target. I want to do string compare of the argument. This is my code,

mode = p
install:
    @echo mode is $(mode)
    ifeq ($(mode),"p")
        @echo mode is production
    else
        @echo mode is development
    endif

I get the following error as,

mode is d
ifeq (d,"p")
/bin/sh: 1: Syntax error: word unexpected (expecting ")")

What is the error and what is General rule for comparing strings in bash scripts?

Smith Dwayne
  • 2,675
  • 8
  • 46
  • 75

1 Answers1

3

You are using pure make syntax (ifeq) as a recipe (the line starts with a tab). Try this, instead:

mode = p
install:
    @echo mode is $(mode)
ifeq ($(mode),p)
    @echo mode is production
else
    @echo mode is development
endif
Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51
  • I got the error `*** missing separator. Stop.` in the line ` @echo mode is development`. – Smith Dwayne Mar 11 '19 at 11:09
  • 1
    Make recipes start with a tab character. Not spaces. Do not just copy-paste the code. Edit it and replace the leading spaces by one tab character. – Renaud Pacalet Mar 11 '19 at 12:34
  • Thank you. This is my input command. `make mode=d install`. My output as follows, `mode is d mode is development ` but for the input `sudo make mode=p install`, It also runs the else statement. My output is, `mode is p mode is development` – Smith Dwayne Mar 13 '19 at 11:07
  • 1
    My fault: I copy-pasted your code and only slightly modified it. I should have been more careful: remove the `"` around `"p"`. Make strings are not `"` delimited. So the `"` characters are part of the string and the comparison between `p` and `"p"` fails. Edited my answer accordingly. – Renaud Pacalet Mar 13 '19 at 12:06
  • I learned something new from you. Sometimes doing fault may be useful for others to learn not to do. :) It works fine. Thank you. – Smith Dwayne Mar 13 '19 at 12:10