Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 读取提示不';在Makefile中运行时无法正确打印_Bash_Makefile - Fatal编程技术网

Bash 读取提示不';在Makefile中运行时无法正确打印

Bash 读取提示不';在Makefile中运行时无法正确打印,bash,makefile,Bash,Makefile,我试图提示用户输入,但在从Makefile或由Makefile启动的子shell中运行时,无法按预期打印read-p提示。以下是我为实现这一目标所做的尝试,但没有成功: test1: @echo '>> before input <<'; \ read -p 'type something:' FOO; \ echo '>> after input <<'; \ echo $$FOO 我尝试

我试图提示用户输入,但在从Makefile或由Makefile启动的子shell中运行时,无法按预期打印
read-p
提示。以下是我为实现这一目标所做的尝试,但没有成功:

test1:
    @echo '>> before input <<'; \
        read -p 'type something:' FOO; \
        echo '>> after input <<'; \
        echo $$FOO
我尝试过的另一种方法是使用Bash的readline接口
read-e

test2:
    @echo '>> before input <<'; \
        read -e -p 'type something:' FOO; \
        echo '>> after input <<'; \
        echo $$FOO
我的输出看起来几乎不错,但输入打印在换行符上:

$ make
>> before input <<
input something:
asdf
>> after input <<
asdf
如果提示没有以
\n
结尾,那么
read
似乎会吃掉提示:

$ make
>> before input <<
asdf
input something:
>> after input <<
asdf
如果它有助于发现问题:OSX10.10.3/Make3.81/Bash3.2.57(1)


免责声明:我知道使用依赖于用户输入的Makefiles不是一个好主意,但我在一个非常特殊的情况下需要它。

对于
测试3
,您可以使用
echo-n
在同一行上打印消息,而不使用尾随的
\n

在您的示例中,您基本上运行的是一个shell命令。可以运行一系列命令,我想这就是您想要的。例如,试试这个。将以下内容放入Makefile:

test1:
    @echo '>> before input <<'
    read -p 'type something: ' FOO; \
    echo '>> after input <<'; \
    echo $$FOO
test1:

@echo'>>在一次又一次的输入之前因为显然没有人能重现我的问题,我想我的环境中一定有什么东西导致了它。我终于发现原因就在
.zshrc
中我的一堆调整中的某个地方

有问题的行原来是一个别名
make
,用于添加带有
grc
的着色,它以某种方式修改了
read
的行为,作为副作用

alias make='grc make'

删除它可以解决所有问题,但最终,我的解决方案是在正常情况下保留别名以进行着色,并在需要用户输入时调用
命令make

首先,我理解,您希望每个Makefile目标都有此行为,而不是每个Makefile都有此行为。我尝试在mac上运行test1,具有所用应用程序的相同版本。我得到的输出是:
bash-3.2$maketest1>>输入前>输入后,我想你已经了解了一些东西,@Ashlaban。注释my
.zshrc
会使read在所有情况下都按预期运行。我会调查导致这个奇怪问题的原因。似乎不起作用。它打印:
-n输入内容:
绕过别名的方法是
命令make
\make
。无意冒犯,但是
/usr/bin/env make
太荒谬了。
$ make
>> before input <<
asdf
input something:
>> after input <<
asdf
test5:
    ./script.sh
test1:
    @echo '>> before input <<'
    read -p 'type something: ' FOO; \
    echo '>> after input <<'; \
    echo $$FOO
alias make='grc make'