在这种特殊情况下,我想在Bash中添加一个确认

Are you sure? [Y/n]

对于Mercurial的hg push ssh://用户名@www.example.com//somepath/morepath,这实际上是一个别名。是否有一个可以添加到别名的标准命令来实现它?

原因是hgpush和hgout听起来很相似,有时当我想要hgoutrepo时,我可能会意外地键入hgpushrepo(两者都是别名)。

更新:如果可以像内置命令一样使用另一个命令,比如:confirm && hg push ssh://…那太好了……只是一个命令,可以询问“是”或“否”,如果是则继续其余的操作。


当前回答

使用回车符可以很容易地绕过确认,并且我发现连续提示有效输入很有用。

这里有一个函数可以简化。如果没有接收到Y|N,则显示红色“无效输入”,并再次提示用户。

prompt_confirm() {
  while true; do
    read -r -n 1 -p "${1:-Continue?} [y/n]: " REPLY
    case $REPLY in
      [yY]) echo ; return 0 ;;
      [nN]) echo ; return 1 ;;
      *) printf " \033[31m %s \n\033[0m" "invalid input"
    esac 
  done  
}

# example usage
prompt_confirm "Overwrite File?" || exit 0

您可以通过传递参数来更改默认提示符

其他回答

这并不完全是一个“询问是或否”,而只是一个hack: alias hg push…不是hgpushrepo,而是hgpushrepoconfirmedpush,当我能把整个事情说清楚的时候,左脑已经做出了一个合乎逻辑的选择。

好吧,这是我的确认版本,修改自James的版本:

function confirm() {
  local response msg="${1:-Are you sure} (y/[n])? "; shift
  read -r $* -p "$msg" response || echo
  case "$response" in
  [yY][eE][sS]|[yY]) return 0 ;;
  *) return 1 ;;
  esac
}

这些变化是:

使用local防止变量名发生冲突 阅读使用$2 $3…为了控制它的动作,可以使用-n和-t 如果读取退出不成功,回显换行美容 我的Git在Windows上只有bash-3.1,没有true或false,所以使用return代替。当然,这也兼容bash-4.4 (Git for Windows的当前版本)。 使用ipython风格的"(y/[n])"明确表示"n"是默认值。

为了避免显式检查'yes'的这些变体,你可以使用bash正则表达式操作符'=~'和一个正则表达式:

read -p "Are you sure you want to continue? <y/N> " prompt
if [[ $prompt =~ [yY](es)* ]]
then
(etc...)

它测试用户输入是否以“y”或“y”开头,后面是0个或多个“es”。

这些是哈米什的回答中更紧凑、更通用的形式。它们可以处理任何大小写字母的混合:

read -r -p "Are you sure? [y/N] " response
case "$response" in
    [yY][eE][sS]|[yY]) 
        do_something
        ;;
    *)
        do_something_else
        ;;
esac

或者,对于Bash >= version 3.2:

read -r -p "Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
then
    do_something
else
    do_something_else
fi

注意:如果$response是一个空字符串,它将给出一个错误。要修复,只需添加引号:"$response"。-在包含字符串的变量中总是使用双引号(例如:更喜欢使用“$@”而不是$@)。

或者,Bash 4.x:

read -r -p "Are you sure? [y/N] " response
response=${response,,}    # tolower
if [[ "$response" =~ ^(yes|y)$ ]]
...

编辑:

作为对你的编辑的回应,以下是你如何根据我回答的第一个版本创建和使用确认命令(它将与其他两个类似):

confirm() {
    # call with a prompt string or use a default
    read -r -p "${1:-Are you sure? [y/N]} " response
    case "$response" in
        [yY][eE][sS]|[yY]) 
            true
            ;;
        *)
            false
            ;;
    esac
}

使用此函数:

confirm && hg push ssh://..

or

confirm "Would you really like to do a push?" && hg push ssh://..

在/etc/bashrc文件中添加以下内容。 这个脚本添加了一个常驻的“函数”,而不是名为“confirm”的别名。


function confirm( )
{
#alert the user what they are about to do.
echo "About to $@....";
#confirm with the user
read -r -p "Are you sure? [Y/n]" response
case "$response" in
    [yY][eE][sS]|[yY]) 
              #if yes, then execute the passed parameters
               "$@"
               ;;
    *)
              #Otherwise exit...
              echo "ciao..."
              exit
              ;;
esac
}