提示用户确认

VBo*_*Boi 4 validation macos bash makefile

我想在运行命令之前获得用户的验证。

我已经尝试了这里所有的方法

.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:
        .....
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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

Run Code Online (Sandbox Code Playgroud)

编辑

尝试过:

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

错误:

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
Run Code Online (Sandbox Code Playgroud)

tha*_*guy 6

Makefile 不是 shell 脚本。每行都在具有单独环境的单独 shell 中运行。

您可以通过在同一行上指定 read 和 'if' 来解决此问题(此处用反斜杠分隔):

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
Run Code Online (Sandbox Code Playgroud)