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

前端 未结 17 3017
轮回少年
轮回少年 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

    Here is roughly a snippet that you want. Let me find out how to forward the arguments.

    read -p "Are you sure you want to continue?  " prompt
    if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]
    then
      # http://stackoverflow.com/questions/1537673/how-do-i-forward-parameters-to-other-command-in-bash-script
    else
      exit 0
    fi
    

    Watch out for yes | command name here :)

提交回复
热议问题