本文翻译自:How do I prompt a user for confirmation in bash script? [duplicate]

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

  • How do I prompt for Yes/No/Cancel input in a Linux shell script? 如何在Linux shell脚本中提示是/否/取消输入? 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楼

参考:https://stackoom.com/question/7uVh/如何在bash脚本中提示用户进行确认-重复


#2楼

This way you get 'y' 'yes' or 'Enter' 通过这种方式,您可以得到“是”或“输入”

 read -r -p "Are you sure? [Y/n]" responseresponse=${response,,} # tolowerif [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; thenyour-action-herefi

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 ]]; thenyour-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]') iny|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?") ]]
thenecho "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的默认变量是$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 x
if [ "$x" = "yes" ]
then# do the dangerous stuff
fi

如何在bash脚本中提示用户进行确认? [重复]相关推荐

  1. linux脚本用户输入,如何在Linux shell脚本中提示用户输入

    本篇文章给大家介绍关于如何在Linux shell脚本中提示用户输入?下面来看具体的内容. 我们首先来看一下命令# read var # read -s "Waiting for input ...

  2. 如何在脚本中输入密码 linux,如何在shell脚本中为用户分配密码

    本篇文章给大家介绍的内容是关于如何在shell脚本中为用户分配密码,下面我们来看具体的内容. 我们先来看一下命令echo | passwd –stdin 例如: 使用以下命令更改shell脚本中用户j ...

  3. linux脚本里用expect,如何在bash脚本中使用expect

    这是我在 following bash脚本中使用的代码片段: for user_input in `awk '{print}' testfile_$$.txt` do ipaddress=`echo ...

  4. 如何在Shell脚本中使用if-else?

    Moving ahead from our previous tutorial on arrays in shell scripts, let's understand how we can use ...

  5. 如何在Bash脚本中将Heredoc写入文件?

    如何在Bash脚本中将Here文档写入文件? #1楼 代替使用cat和I / O重定向,可以使用tee代替: tee newfile <<EOF line 1 line 2 line 3 ...

  6. linux sftp账号密码脚本传文件,关于shell:如何使用Bash脚本中的密码运行sftp命令?...

    我需要从Linux主机使用sftp将日志文件传输到远程主机.我的操作组已为我提供了相同的凭据.但是,由于我无法控制其他主机,因此无法生成RSA密钥并与其他主机共享. 那么,有没有一种方法可以通过cro ...

  7. 如何在Bash脚本中将DOS / Windows换行符(CRLF)转换为Unix换行符(LF)?

    本文翻译自:How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script? How can I pro ...

  8. linux脚本中如何读取文件,如何在Shell脚本中逐行读取文件

    原标题:如何在Shell脚本中逐行读取文件 在这里,我们学习Shell脚本中的3种方法来逐行读取文件. 方法一.使用输入重定向 逐行读取文件的最简单方法是在while循环中使用输入重定向. 为了演示, ...

  9. java中bash应用_在bash脚本中查找java应用程序的进程ID(以查看目标应用程序是否已在运行)...

    我知道获取进程ID有一百万个问题,但这个问题似乎是独一无二的.谷歌没有给我答案,所以我希望stackexhange会帮助而不是关闭这个问题. 当涉及Java时,找到进程ID似乎比较棘手(pgrep不起 ...

最新文章

  1. html的后绑定事件,HTML 控件绑定事件
  2. revit建筑样板_Revit出建筑施工图步骤及注意事项
  3. linux 基础学习入门 2
  4. 转帖:关于MongoDB你需要知道的几件事
  5. dijkstra算法原理_这 10 大基础算法,程序员必知必会!
  6. 我所理解的设计模式(C++实现)—— “一句话总结”和索引
  7. 现实地形导入UE4全流程
  8. 华为手机android版本升级失败怎么办,华为手机系统更新好吗 华为手机系统更新方法...
  9. [JSOI2009]球队收益
  10. windows生成当前目录树
  11. 自然辩证法 题目2
  12. 二十九 Python分布式爬虫打造搜索引擎Scrapy精讲—selenium模块是一个python操作浏览器软件的一个模块,可以实现js动态网页请求...
  13. STM32学习(1)-资料查找,STM32简介,STM32选型以及芯片内部结构图
  14. cdrx8如何批量导出jpg_cdrx8如何批量导出jpg_办公软件操作技巧022:如何从word文档中批量导出多张图片......
  15. win10文件索引服务器,Win10系统修改索引文件夹路径的方法
  16. 三菱q plc modbus通讯协议详解_三菱Q系列PLC与昆仑通态触摸屏以太网通讯
  17. linux 输入两个命令,Linux两条命令touch、vi
  18. 四个方面讲解MPK(安规电容)与CBB电容的区别
  19. 视觉惯性组合导航技术最新综述:应用优势、主要类别及一种视觉惯性组合导航无人系统开发验证平台分享
  20. 【webrtc】 CongestionControlHandler 的 RTC_DCHECK_RUN_ON(sequenced_checker_);

热门文章

  1. AsyncQueryHandler 异步查询框架
  2. 高处看Surface,WIndow,View,SurfaceView
  3. 算法---------寻找重复的子树(Java版本)
  4. 读取excel日期 c++_实例9:用Python自动生成Excel档每日出货清单
  5. Worker启动Executor源码
  6. 编码区和非编码区的关系
  7. 【原】DjianGo Windows7下的安装
  8. JDBC——jdbcUtils加载配置文件赋值
  9. [poj3041]Asteroids(二分图的最小顶点覆盖)
  10. Docker学习笔记(4) — 开启Docker远程访问