我想在一个有潜在危险的bash脚本的顶部快速提示“你确定吗?”以进行确认,最简单/最好的方法是什么?


当前回答

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

我是一个在bash中读取并回显结果的简短脚本。

其他回答

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

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

在函数中使用此选项查找现有文件并在覆盖之前提示。

用例/esac。

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

优点:

更整洁的可以更容易地使用“OR”条件可以使用字符范围,例如[yY][eE][sS]来接受单词“yes”,其中任何字符都可以是小写或大写。

这样您就可以得到“y”、“yes”或“Enter”

 read -r -p "Are you sure? [Y/n]" response
 response=${response,,} # tolower
 if [[ $response =~ ^(y| ) ]] || [[ -z $response ]]; then
    your-action-here
 fi

如果您正在使用zsh,请尝试以下操作:

read "response?Are you sure ? [Y/n] "
response=${response:l} #tolower
if [[ $response =~ ^(y| ) ]] || [[ -z $response ]]; then
    your-action-here
fi

这是我在其他地方找到的,有更好的版本吗?

read -p "Are you sure you wish to continue?"
if [ "$REPLY" != "yes" ]; then
   exit
fi
#!/bin/bash
echo Please, enter your name
read NAME
echo "Hi $NAME!"
if [ "x$NAME" = "xyes" ] ; then
 # do something
fi

我是一个在bash中读取并回显结果的简短脚本。