Basic if else statement in Makefile

后端 未结 3 1705
灰色年华
灰色年华 2020-12-21 09:52

I\'m trying to execute a simple if else statement in a Makefile:

check:
  if [ -z \"$(APP_NAME)\" ]; then \\
    echo \"Empty\" \\
  else \\
    echo \"Not e         


        
3条回答
  •  执笔经年
    2020-12-21 10:23

    This is shell syntax, not makefiles. You need to familiarize yourself with the rules surrounding using backslashes to enter long commands into a single line of shell.

    In your example, after the backslash newline pairs are removed, it looks like this:

    if [ -z "$(APP_NAME)" ]; then echo "Empty" else echo "Not empty" fi
    

    Maybe you can now see that the issue is. The shell interprets that as:

    if [ -z "$(APP_NAME)" ]; then
    

    followed by a single long command:

    echo "Empty" else echo "Not empty" fi
    

    which would echo the content Empty else echo not empty fi, except that since there's no trailing fi shell token it's instead a syntax error.

    In shell syntax you need to add a semicolon after every individual command, so the shell knows how to split it up:

    check:
            if [ -z "$(APP_NAME)" ]; then \
                echo "Empty"; \
            else \
                echo "Not empty"; \
            fi
    

    Note the semicolons after the echo commands telling the shell that the command arguments end there.

提交回复
热议问题