本文翻译自:How do I prompt a user for confirmation in bash script? [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
I want to put a quick "are you sure?" 我想快点说“你确定吗?” prompt for confirmation at the top of a potentially dangerous bash script, what's the easiest/best way to do this? 提示在潜在危险的bash脚本顶部进行确认,最简单/最好的方法是什么?
#1楼
参考:https://stackoom.com/question/7uVh/如何在bash脚本中提示用户进行确认-重复
#2楼
This way you get 'y' 'yes' or 'Enter' 通过这种方式,您可以得到“是”或“输入”
read -r -p "Are you sure? [Y/n]" response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
your-action-here
fi
If you are using zsh try this: 如果你使用zsh试试这个:
read "response?Are you sure ? [Y/n] "
response=${response:l} #tolower
if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
your-action-here
fi
#3楼
Here's the function I use : 这是我使用的功能:
function ask_yes_or_no() {
read -p "$1 ([y]es or [N]o): "
case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
y|yes) echo "yes" ;;
*) echo "no" ;;
esac
}
And an example using it: 并使用它的一个例子:
if [[ "no" == $(ask_yes_or_no "Are you sure?") || \
"no" == $(ask_yes_or_no "Are you *really* sure?") ]]
then
echo "Skipped."
exit 0
fi
# Do something really dangerous...
- The output is always "yes" or "no" 输出始终为“是”或“否”
- It's "no" by default 默认情况下它是“否”
- Everything except "y" or "yes" returns "no", so it's pretty safe for a dangerous bash script 除“y”或“yes”之外的所有内容都返回“no”,因此对于危险的bash脚本来说非常安全
- And it's case insensitive, "Y", "Yes", or "YES" work as "yes". 它不区分大小写,“Y”,“是”或“是”的工作为“是”。
I hope you like it, 我希望你喜欢它,
Cheers! 干杯!
#4楼
qnd: use qnd:使用
read VARNAME
echo $VARNAME
for a one line response without readline support. 对于没有readline支持的单行响应。 Then test $VARNAME however you want. 然后根据需要测试$ VARNAME。
#5楼
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
# do dangerous stuff
fi
I incorporated levislevis85 's suggestion (thanks!) and added the -n
option to read
to accept one character without the need to press Enter . 我收录了levislevis85的建议(谢谢!)并添加-n
选项以read
接受一个字符而无需按Enter键 。 You can use one or both of these. 您可以使用其中一个或两个。
Also, the negated form might look like this: 此外,否定的形式可能如下所示:
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi
However, as pointed out by Erich, under some circumstances such as a syntax error caused by the script being run in the wrong shell, the negated form could allow the script to continue to the "dangerous stuff". 然而,正如Erich所指出的,在某些情况下,例如由于脚本在错误的shell中运行而导致的语法错误,否定的形式可能允许脚本继续“危险的东西”。 The failure mode should favor the safest outcome so only the first, non-negated if
should be used. 失效模式应有利于最安全的结果,所以只有第一,非否定if
应该被使用。
Explanation: 说明:
The read
command outputs the prompt ( -p "prompt"
) then accepts one character ( -n 1
) and accepts backslashes literally ( -r
) (otherwise read
would see the backslash as an escape and wait for a second character). read
命令输出提示符( -p "prompt"
)然后接受一个字符( -n 1
)并按字面接受反斜杠( -r
)(否则read
会将反斜杠视为转义并等待第二个字符)。 The default variable for read
to store the result in is $REPLY
if you don't supply a name like this: read -p "my prompt" -n 1 -r my_var
如果你不提供这样的名字,那么用于存储结果的read
的默认变量是$REPLY
: read -p "my prompt" -n 1 -r my_var
The if
statement uses a regular expression to check if the character in $REPLY
matches ( =~
) an upper or lower case "Y". if
语句使用正则表达式来检查$REPLY
中的字符是否匹配( =~
)大写或小写“Y”。 The regular expression used here says "a string starting ( ^
) and consisting solely of one of a list of characters in a bracket expression ( [Yy]
) and ending ( $
)". 这里使用的正则表达式表示“字符串开头( ^
),仅包含括号表达式( [Yy]
)和结束( $
)中的一个字符列表”。 The anchors ( ^
and $
) prevent matching longer strings. 锚点( ^
和$
)阻止匹配更长的字符串。 In this case they help reinforce the one-character limit set in the read
command. 在这种情况下,它们有助于强化read
命令中设置的单字符限制。
The negated form uses the logical "not" operator ( !
) to match ( =~
) any character that is not "Y" or "y". 否定形式使用逻辑“非”运算符( !
)来匹配( =~
)任何不是“Y”或“y”的字符。 An alternative way to express this is less readable and doesn't as clearly express the intent in my opinion in this instance. 表达这种情况的另一种方式是可读性较差,并且在这种情况下不能清楚地表达我的意图。 However, this is what it would look like: if [[ $REPLY =~ ^[^Yy]$ ]]
但是,这就是它的样子: if [[ $REPLY =~ ^[^Yy]$ ]]
#6楼
echo are you sure?
read x
if [ "$x" = "yes" ]
then
# do the dangerous stuff
fi