提示用户进行确认

时间:2019-05-08 21:53:48

标签: bash macos validation makefile

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

我尝试了所有方法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:
        .....

我收到以下错误:

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

编辑

尝试过:

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

错误:

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

1 个答案:

答案 0 :(得分:1)

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