0

I have one use case to iterate arguments which I pass via a make command as shown below code, if you can see x=$(filter-out $@,$(MAKECMDGOALS)) in Makefile I am getting below error:

Error:

$ make plan ab1,ab2
TARGET=""
x=ab1,ab2
echo 
for i in 
/bin/sh: 1: Syntax error: end of file unexpected
make: *** [Makefile:9: plan] Error 2

MakeFile

SHELL := /bin/sh

all: plan

plan:
    TARGET=""
    x=$(filter-out $@,$(MAKECMDGOALS))
    echo $x
    for i in ${x//,/ }
    do
    TARGET="$TARGET -target=module.abc$i"
    done
    echo $TARGET
    terraform plan $TARGET
    
%:      # The target name % means that it is a rule that matches anything
    @:    # The @: is a recipe; the : means do nothing, and the @ means do it silently.

calling command

make plan ab1,ab2

Please help me to optimize my Makefile, and also please do let me know about assigning $(filter-out $@,$(MAKECMDGOALS)) into a variable.

Thanks in Advance.

  • 1
    You can see from your output that the `filter-out` function works fine, and gives you back the results that I assume you want: `x=ab1,ab2`. It's the later line `for i in` that causes the problem. See the linked question and answer for why. – MadScientist May 11 '22 at 18:04
  • Also, you are setting `SHELL := /bin/sh` in your makefile to request a POSIX-conforming shell, but then in your script you use regex variable replacement `${x//,/ }` which is a bash-specific feature and not available in POSIX shells. If you want to require bash, you should say `SHELL := /bin/bash`. Else you should change your recipe to not rely on bash features. – MadScientist May 11 '22 at 18:06
  • @MadScientist assigning ```$(filter-out $@,$(MAKECMDGOALS))``` into x variable is not working here. if you see an output, there is nothing to ```echo``` Thanks I added SHELL := /bin/bash – ADITYA CHOUDHARI May 11 '22 at 18:13
  • @MadScientist also I tried ```x=`echo $(filter-out $@,$(MAKECMDGOALS))` echo $x``` – ADITYA CHOUDHARI May 11 '22 at 18:16
  • 1
    The reason the echo is empty is the same as why you get the syntax error. You've assigned the shell variable in one logical line, then the shell exits and the value of that variable is gone. Then make starts a new shell to run the next logical line, `echo $x`, but in the shell the shell variable `x` has no value. – MadScientist May 11 '22 at 18:25

0 Answers0