gpt4 book ai didi

bash - 提示用户确认

转载 作者:行者123 更新时间:2023-12-04 12:42:53 24 4
gpt4 key购买 nike

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

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

最佳答案

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

您可以通过在同一行(此处用反斜杠分隔)指定读取和“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

关于bash - 提示用户确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56049535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com