简体   繁体   English

Makefile multiline dash命令在分离的进程中运行可执行文件

[英]Makefile multiline dash command run executable in a detached process

I have the following target in my makefile: (I'd like to run python http server in a detached process and when bash script is done kill the server) 我的makefile中有以下目标:(我想在一个分离的进程中运行python http服务器,当bash脚本完成时杀死服务器)

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 an error 但是,当我把一个&后行python ...我得到一个错误

/bin/dash: Syntax error: ";" / bin / dash:语法错误:“;” unexpected 意外

How can this be done in a proper way? 怎么能以适当的方式完成?

EDIT 编辑

I have changed my makefile to do the following: 我已经改变了我的makefile来执行以下操作:

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 am getting an error: (without the line number) 但是我收到一个错误:(没有行号)

/bin/dash: Syntax error: word unexpected / bin / dash:语法错误:单词意外

The important thing to remember here is that your line breaks don't actually exist when the shell sees the command. 这里要记住的重要一点是,当shell看到命令时,你的换行符实际上并不存在。

So your first command becomes: 所以你的第一个命令变为:

$(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 becomes: 你的第二个命令变为:

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 those are both very hard to read so I don't expect you to spot the problem but the problem is that you are missing statement terminators in a few places. 现在这些都很难读,所以我不指望你发现问题,但问题是你在一些地方缺少语句终止符。

Specifically: 特别:

  • Braces ( {} ) are word elements and so need spaces around them (and a terminator before, and after, the closing brace). 大括号( {} )是单词元素,因此需要它们周围的空格(以及结束大括号之前和之后的终止符)。 You are missing those terminators here fi } trap and here fi } echo . 你在这里错过了那些终结者。 fi } trap和这里fi } echo

  • fi is also not a statement terminator and so it needs one between it and the next statement. fi也不是语句终止符,因此它需要在它和下一个语句之间。 You are missing one here test.py; fi function 你在这里缺少一个test.py; fi function test.py; fi function (as well as the ones in the braces from the first point). test.py; fi function (以及第一点中括号中的test.py; fi function )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM