In Bash, how to add “Are you sure [Y/n]” to any command or alias?

前端 未结 17 3014
轮回少年
轮回少年 2020-11-29 15:11

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://username@www.example.com//some

17条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 15:34

    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://..
    

提交回复
热议问题