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

前端 未结 17 3016
轮回少年
轮回少年 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条回答
  •  旧时难觅i
    2020-11-29 15:16

    Add the following to your /etc/bashrc file. This script adds a resident "function" instead of an alias called "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
    }
    

提交回复
热议问题