4

I want to get a user's validation before running a command.

I've tried all methods here

.PHONY: rebuild validate

rebuild:
    @echo "rebuilding cluster to previous stable state"
    @echo "Do you wish to continue (y/n)?"
    select yn in "Yes" "No"
        case $yn in
            Yes ) make validate;;
            No ) exit;;
    esac
validate:
        .....

I get the following error:

rebuilding cluster to previous stable state
Do you wish to continue (y/n)?
select yn in "Yes" "No"
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [rebuild] Error 2

EDIT

Tried :

rebuild:
    @echo "rebuilding cluster to previous stable state"
    @read -p "Are you sure? " -n 1 -r
    @echo    
    if [[ REPLY =~ ^[Yy] ]]
    then
        make validate
    fi  

Errors with:

rebuilding cluster to previous stable state
Are you sure? y
if [[ REPLY =~ ^[Yy] ]]
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [rebuild] Error 2
VBoi
  • 349
  • 5
  • 21

1 Answers1

6

Makefiles are not shell scripts. Each line runs in a separate shell with a separate environment.

You can work around this by specifying the read and 'if' on the same line (here broken with backslashes):

SHELL=bash
rebuild:
        @echo "rebuilding cluster to previous stable state"
        @read -p "Are you sure? " -n 1 -r; \
        if [[ $$REPLY =~ ^[Yy] ]]; \
        then \
            make validate; \
        fi
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • ... alternatively, you can put the whole command on one physical line, with appropriate use of semicolons. In the OP's `case` variation, too. – John Bollinger May 09 '19 at 00:34