Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read prompt doesn't print correctly when run within Makefile

Tags:

bash

makefile

I'm trying to prompt user input but I'm having trouble to get read -p prompt getting printed as expected when run from within a Makefile or a subshell started by a Makefile. Here are my attempts on achieving this without success:

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

The output looks like this assuming I input asdf. My input gets printed as I type, but the prompt type something: gets printed after everything else:

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

Another way I've tried is using Bash's readline interface read -e:

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 prompt type something: nor the actual input as I type get printed until after I have pressed ENTER, which is quite inconvenient when asking for input.

I also tried printing the prompt before with echo:

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

And my output looks almost good, but the input is printed on a newline:

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

One final tweak using printf to avoid the newline:

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

And it seems read eats away the prompt if it doesn't end with \n:

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

And, of course, exactly the same happens if I simply call a script:

test5:
    ./script.sh

In case it helps spotting the problem: OS X 10.10.3 / make 3.81 / bash 3.2.57(1).

Disclaimer: I know it's not a good idea to have user input dependant Makefiles, but I need this for a very particular case.

like image 539
OmeGak Avatar asked Jun 07 '15 12:06

OmeGak


1 Answers

Since apparently nobody could reproduce my issue, I figured I had to have something going on on my environment causing it. I finally found the cause was lurking somewhere on my pile of tweaks within .zshrc.

The offending line turns out to be an aliased make for adding colorization with grc that somehow modifies the behavior of read as a side effect.

alias make='grc make'

Removing it solves all the issues, but, in the end, my solution is to keep the alias for colorization in normal circumstances and simply invoke command make when user input is required.

like image 148
OmeGak Avatar answered Nov 15 '22 05:11

OmeGak