本文介绍了在 Bash 中,如何添加“Are you sure [Y/n]"?任何命令或别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

你确定吗?[是/否]

对于Mercurial的hg push ssh://[email protected]//somepath/morepath,其实就是一个别名.有没有标准命令可以加别名来实现?

原因是hg pushhg out听起来很相似,有时当我想要hgoutrepo时,我可能会不小心输入hgpushrepo(都是别名).

更新:如果它可以是带有另一个命令的内置命令,例如:confirm &&hg push ssh://... 那会很棒...只是一个命令,可以要求 yesno 并继续如果yes,则休息.

解决方案

这些是 Hamish 的答案.它们处理大小写字母的任何混合:

read -r -p "Are you sure? [y/N] " response案例$response"在[yY][eE][sS]|[yY])做点什么;;*)做其他事;;esac

或者,对于 Bash >= 3.2 版:

read -r -p "Are you sure? [y/N] " responseif [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]然后做点什么别的做其他事菲

注意:如果$response 为空字符串,则会报错.要修复,只需添加引号:"$response".– 始终在包含字符串的变量中使用双引号(例如:更喜欢使用 "$@" 而不是 $@).

或者,Bash 4.x:

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

为了响应您的编辑,以下是您将如何根据我的答案中的第一个版本创建和使用 confirm 命令(它与其他两个类似):

confirm() {# 使用提示字符串调用或使用默认值阅读 -r -p "${1:-Are you sure? [y/N]} " 响应案例$response"在[yY][eE][sS]|[yY])真的;;*)错误的;;esac}

使用此功能:

确认&&hg 推送 ssh://..

确认你真的想要推送吗?"&&hg 推送 ssh://..

In this particular case, I'd like to add a confirm in Bash for

Are you sure? [Y/n]

for Mercurial's hg push ssh://[email protected]//somepath/morepath, which is actually an alias. Is there a standard command that can be added to the alias to achieve it?

The reason is that hg push and hg out can sound similar and sometimes when I want hgoutrepo, I may accidentlly type hgpushrepo (both are aliases).

Update: if it can be something like a built-in command with another command, such as: confirm && hg push ssh://... that'd be great... just a command that can ask for a yes or no and continue with the rest if yes.

解决方案

These are more compact and versatile forms of Hamish's answer. They handle any mixture of upper and lower case letters:

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

Or, for 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

Note: If $response is an empty string, it will give an error. To fix, simply add quotation marks: "$response". – Always use double quotes in variables containing strings (e.g.: prefer to use "$@" instead $@).

Or, Bash 4.x:

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

Edit:

In response to your edit, here's how you'd create and use a confirm command based on the first version in my answer (it would work similarly with the other two):

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
}

To use this function:

confirm && hg push ssh://..

or

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

这篇关于在 Bash 中,如何添加“Are you sure [Y/n]"?任何命令或别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:49