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

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

    To avoid explicitly checking for these variants of 'yes' you could use the bash regular expression operator '=~' with a regular expression:

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

    That tests whether the user input starts with 'y' or 'Y' and is followed by zero or more 'es's.

提交回复
热议问题