Makefile multiline dash runs the executable in a separate process

I have the following target in my makefile: (I would like to start the python http server in a detached process and when the bash script is finished kill the server)

TEST_PORT = 17777
test::
    $(ENV_VARS) \
    python -m SimpleHTTPServer $(TEST_PORT); \
    PID=$$(lsof -t -i @localhost:$(TEST_PORT) -sTCP:listen); \
    echo $(PID); \
    if [ -n "$$PID" ]; \
    then \
        python test.py; \
    fi; \
    function finish { \
        if [ -n "$$PID" ]; \
        then \
            kill -9 $$PID; \
        fi \
    } \
    trap finish EXIT;

      

However, when I put a &

after the line python ...

, I get the error

/ bin / dash: Syntax error: ";" Unexpected

How can this be done properly?

EDIT

I modified my makefile to do the following:

test::
    python -m SimpleHTTPServer $(TEST_PORT) &
    PID=$$(lsof -t -i @localhost:$(TEST_PORT) -sTCP:listen); \
        if [ -n "$$PID" ]; \
        then \
            $(ENV_VARS) python test.py; \
        fi \
        function finish { \
            if [ -n "$$PID" ]; \
            then \
                kill -9 $$PID; \
            fi \
        } \
        echo $$PID; \
        trap finish EXIT;

      

However, I get the error: (no line number)

/ bin / dash: Syntax error: word unexpected

+3


source to share


1 answer


It's important to remember that line breaks don't actually exist when the shell sees the command.

So your first command would be:

$(ENV_VARS) python -m SimpleHTTPServer $(TEST_PORT); PID=$$(lsof -t -i @localhost:$(TEST_PORT) -sTCP:listen); echo $(PID); if [ -n "$$PID" ]; then python test.py; fi; function finish { if [ -n "$$PID" ]; then kill -9 $$PID; fi } trap finish EXIT;

      

And your second command would be:



PID=$$(lsof -t -i @localhost:$(TEST_PORT) -sTCP:listen); if [ -n "$$PID" ]; then $(ENV_VARS) python test.py; fi function finish { if [ -n "$$PID" ]; then kill -9 $$PID; fi } echo $$PID; trap finish EXIT;

      

Now they are very difficult to read, so I don't expect you to find the problem, but the problem is that you are losing terminal terminators in several places.

In particular:

  • The brackets ( {}

    ) are elements of a word and therefore need spaces (and a terminator before and after the closing curly brace). You are missing these terminators fi } trap

    here as well fi } echo

    .

  • fi

    is also not an operator terminator and so it needs one between it and the next statement. You're missing here test.py; fi function

    (as well as the ones in curly braces from the first dot).

+2


source







All Articles