如何提示用户在bash脚本中进行确认?(复制)

时间:2021-07-05 15:46:41

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脚本的顶部提示确认,最简单/最好的方法是什么?

10 个解决方案

#1


634  

read -p "Are you sure? " -n 1 -r
echo    # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
    # do dangerous stuff
fi

Edit:

编辑:

I incorporated levislevis85's suggestion (thanks!) and added the -n option to read to accept one character without the need to press Enter. You can use one or both of these.

我加入了levislevis85的建议(谢谢!),并添加了-n选项来读取一个字符,而不需要按Enter键。你可以使用其中一个或两个。

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". The failure mode should favor the safest outcome so only the first, non-negated if should be used.

然而,正如Erich所指出的,在某些情况下,例如由于脚本在错误的shell中运行而导致的语法错误,被否定的表单将允许脚本继续执行“危险的内容”。失败模式应该有利于最安全的结果,所以只有第一个,非否定的应该被使用。

#2


107  

use case/esac.

用例/ esac。

read -p "Continue (y/n)?" choice
case "$choice" in 
  y|Y ) echo "yes";;
  n|N ) echo "no";;
  * ) echo "invalid";;
esac

advantage:

优势:

  1. neater
  2. 整洁
  3. can use "OR" condition easier
  4. 是否可以使用“或”条件?
  5. can use character range, eg [yY][eE][sS] to accept word "yes", where any of its characters may be in lowercase or in uppercase.
  6. 可以使用字符范围,如[yY][eE][sS]接受单词“yes”,其中任何字符都可以是小写字母或大写字母。

#3


23  

Try the read shell builtin:

试一试阅读shell builtin:

read -p "Continue (y/n)?" CONT
if [ "$CONT" = "y" ]; then
  echo "yaaa";
else
  echo "booo";
fi

#4


23  

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

#5


17  

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”,“Yes”或者“Yes”的工作是“Yes”。

I hope you like it,
Cheers!

我希望你喜欢,干杯!

#6


11  

This what I found elsewhere, is there a better possible version?

这是我在别处发现的,有没有更好的版本?

read -p "Are you sure you wish to continue?"
if [ "$REPLY" != "yes" ]; then
   exit
fi

#7


4  

[[ -f ./${sname} ]] && read -p "File exists. Are you sure? " -n 1

[[ ! $REPLY =~ ^[Yy]$ ]] && return 1

used this in a function to look for an existing file and prompt before overwriting.

用于在函数中查找现有文件并在重写之前提示。

#8


3  

#!/bin/bash
echo Please, enter your name
read NAME
echo "Hi $NAME!"
if [ "x$NAME" = "xyes" ] ; then
 # do something
fi

I s a short script to read in bash and echo back results.

在bash中读取一个短脚本并返回结果。

#9


2  

qnd: use

qnd:使用

read VARNAME
echo $VARNAME

for a one line response without readline support. Then test $VARNAME however you want.

对于一个没有readline支持的行响应。然后测试$VARNAME。

#10


2  

echo are you sure?
read x
if [ "$x" = "yes" ]
then
  # do the dangerous stuff
fi

#1


634  

read -p "Are you sure? " -n 1 -r
echo    # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
    # do dangerous stuff
fi

Edit:

编辑:

I incorporated levislevis85's suggestion (thanks!) and added the -n option to read to accept one character without the need to press Enter. You can use one or both of these.

我加入了levislevis85的建议(谢谢!),并添加了-n选项来读取一个字符,而不需要按Enter键。你可以使用其中一个或两个。

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". The failure mode should favor the safest outcome so only the first, non-negated if should be used.

然而,正如Erich所指出的,在某些情况下,例如由于脚本在错误的shell中运行而导致的语法错误,被否定的表单将允许脚本继续执行“危险的内容”。失败模式应该有利于最安全的结果,所以只有第一个,非否定的应该被使用。

#2


107  

use case/esac.

用例/ esac。

read -p "Continue (y/n)?" choice
case "$choice" in 
  y|Y ) echo "yes";;
  n|N ) echo "no";;
  * ) echo "invalid";;
esac

advantage:

优势:

  1. neater
  2. 整洁
  3. can use "OR" condition easier
  4. 是否可以使用“或”条件?
  5. can use character range, eg [yY][eE][sS] to accept word "yes", where any of its characters may be in lowercase or in uppercase.
  6. 可以使用字符范围,如[yY][eE][sS]接受单词“yes”,其中任何字符都可以是小写字母或大写字母。

#3


23  

Try the read shell builtin:

试一试阅读shell builtin:

read -p "Continue (y/n)?" CONT
if [ "$CONT" = "y" ]; then
  echo "yaaa";
else
  echo "booo";
fi

#4


23  

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

#5


17  

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”,“Yes”或者“Yes”的工作是“Yes”。

I hope you like it,
Cheers!

我希望你喜欢,干杯!

#6


11  

This what I found elsewhere, is there a better possible version?

这是我在别处发现的,有没有更好的版本?

read -p "Are you sure you wish to continue?"
if [ "$REPLY" != "yes" ]; then
   exit
fi

#7


4  

[[ -f ./${sname} ]] && read -p "File exists. Are you sure? " -n 1

[[ ! $REPLY =~ ^[Yy]$ ]] && return 1

used this in a function to look for an existing file and prompt before overwriting.

用于在函数中查找现有文件并在重写之前提示。

#8


3  

#!/bin/bash
echo Please, enter your name
read NAME
echo "Hi $NAME!"
if [ "x$NAME" = "xyes" ] ; then
 # do something
fi

I s a short script to read in bash and echo back results.

在bash中读取一个短脚本并返回结果。

#9


2  

qnd: use

qnd:使用

read VARNAME
echo $VARNAME

for a one line response without readline support. Then test $VARNAME however you want.

对于一个没有readline支持的行响应。然后测试$VARNAME。

#10


2  

echo are you sure?
read x
if [ "$x" = "yes" ]
then
  # do the dangerous stuff
fi