Read prompt does not print correctly when run in Makefile

I'm trying to request user input, but it's hard for me to get a read -p prompt that will print as expected when I run from the Makefile or subshell launched by the Makefile. Here are my attempts to achieve this without success:

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

The result is as follows: I enter asdf . My input prints as I type, but the hint type something: prints after everything else:

 $ make >> before input << asdf >> after input << asdf type something 

Another way I tried is using the Bash of the readline read -e interface:

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

In this case, the output looks good, however, neither the hint of type something: nor the actual input, as I type, is printed until I press ENTER , which is rather inconvenient when prompting for input.

I also tried to print the invitation before echo :

 test3: @echo '>> before input <<'; \ echo 'input something:';\ read FOO; \ echo '>> after input <<'; \ echo $$FOO;\ 

And my output looks pretty good, but the input is printed on a new line:

 $ make >> before input << input something: asdf >> after input << asdf 

Last final using printf to avoid a new line:

 test4: @echo '>> before input <<'; \ printf 'input something: ';\ read FOO; \ printf '\n'; \ echo '>> after input <<'; \ echo $$FOO;\ 

And it seems that read throws a prompt if it doesn't end with \n :

 $ make >> before input << asdf input something: >> after input << asdf 

And of course, exactly the same thing happens if I just call the script:

 test5: ./script.sh 

In case this helps identify the problem: OS X 10.10.3 / make 3.81 / Bash 3.2.57 (1).

Disclaimer: I know that it’s not very important to have user-dependent Makefiles, but I need this for a special case.

+6
source share
3 answers

Since, apparently, no one could reproduce my problem, I thought that something had to happen in my environment that caused it. Finally, I found that the reason is hiding somewhere on my settings heap in .zshrc .

The insult line turns out to be an alias to make to add coloring with grc , which somehow modifies read behavior as a side effect.

 alias make='grc make' 

Removing this solution solves all the problems, but ultimately my solution is to keep the alias for coloring under normal circumstances and just call command make when user input is required.

0
source

For test 3 you can use echo -n to print your message on the same line without the end \n

0
source

In your examples, you use basically one shell command. You can run a number of commands, and I think you want it here. For example, try this. Paste the Makefile:

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

Note that there is no semicolon and backslash in the first @echo.

0
source

Source: https://habr.com/ru/post/988646/


All Articles