代码之家  ›  专栏  ›  技术社区  ›  balupton

makefile-为什么read命令不读取用户输入?

  •  15
  • balupton  · 技术社区  · 14 年前

    我在makefile中有以下代码:

    # Root Path
    echo "What is the root directory of your webserver? Eg. ~/Server/htdocs" ;
    read root_path ;
    echo $root_path ;
    if [ ! -d $root_path ] ; then \
        echo "Error: Could not find that location!" ; exit 1 ; \
    fi
    

    但是,当输入任何内容(如“asd”)时,返回的内容是:

    What is the root directory of your webserver? Eg. ~/Server/htdocs
    
    asd
    oot_path
    Error: Could not find that location!
    

    我希望看到的是:

    What is the root directory of your webserver? Eg. ~/Server/htdocs
    
    asd
    asd
    Error: Could not find that location!
    

    我该怎么解决这个问题????

    1 回复  |  直到 14 年前
        1
  •  21
  •   Greg Hewgill    14 年前

    最直接的问题是,它自己解释了 $ 与外壳不同。尝试:

        echo "What is the root directory of your webserver? Eg. ~/Server/htdocs"; \
        read root_path; \
        echo $$root_path
    

    $$ 逃逸 $ 为了制造,所以它通过了单曲 $ 一直到外壳。还要注意,您需要使用 \ 行继续,以便整个序列作为一个shell脚本执行,否则make将生成一个 新的 每行的外壳。这意味着任何你 read 它的外壳一离开就会消失。

    我也会说,一般来说,提示来自makefile的交互式输入是不常见的。最好使用命令行开关来指示Web服务器根目录。