提示用户确认

4

在运行命令之前,我希望能够获得用户的确认。

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

.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
呸!不要在 makefile 中提示。超级奇怪的。 - John Kugelman
我想运行的其中一个命令非常危险,因此我希望在此之前进行检查。因此,我需要一个提示。 - VBoi
1个回答

6

Makefiles不是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

或者,您可以将整个命令放在一行物理行上,并适当使用分号。在 OP 的 case 变体中也是如此。 - John Bollinger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接