博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在bash脚本中提示用户进行确认? [重复]
阅读量:2289 次
发布时间:2019-05-09

本文共 4357 字,大约阅读时间需要 14 分钟。

本文翻译自:

This question already has an answer here: 这个问题在这里已有答案:

  • 29 answers 29个答案

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楼

参考:


#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} #tolowerif [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then    your-action-herefi

#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 0fi# 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 VARNAMEecho $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 -recho    # (optional) move to a new lineif [[ $REPLY =~ ^[Yy]$ ]]then    # do dangerous stufffi

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 -recho    # (optional) move to a new lineif [[ ! $REPLY =~ ^[Yy]$ ]]then    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shellfi

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的默认变量是$REPLYread -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 xif [ "$x" = "yes" ]then  # do the dangerous stufffi

转载地址:http://xicnb.baihongyu.com/

你可能感兴趣的文章
Zend_Db -> Zend_Db_Table
查看>>
PHP漏洞全解(三)-客户端脚本植入
查看>>
Java学习日记 求最值 排序 选择 冒泡 交换
查看>>
IO流输出系统信息
查看>>
File类常见方法
查看>>
File对象功能-文件列表
查看>>
IO流列出目录下所有内容,带层次
查看>>
IO流删除带内容的目录
查看>>
IO流创建java文件列表
查看>>
Properties存取配置文件
查看>>
记录应用程序运行次数
查看>>
打印流PrintStream和PrintWriter
查看>>
IO流 合并流 SequenceInputStream
查看>>
IO流切割文件
查看>>
IO流-对象序列化操作流
查看>>
io流-多线程连接管道流
查看>>
RandomAccessFile可实现数据的分段写入也就是多线程下载
查看>>
DataInputStream与DataOutputStream用于操作基本数据类型的数据的流对象
查看>>
ByteArrayStream用于操作字节数组的流对象
查看>>
IO流-字符编码表转换示例
查看>>