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

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

    This may be a little too short, but for my own private use, it works great

    read -n 1 -p "Push master upstream? [Y/n] " reply; 
    if [ "$reply" != "" ]; then echo; fi
    if [ "$reply" = "${reply#[Nn]}" ]; then
        git push upstream master
    fi
    

    The read -n 1 just reads one character. No need to hit enter. If it's not a 'n' or 'N', it is assumed to be a 'Y'. Just pressing enter means Y too.

    (as for the real question: make that a bash script and change your alias to point to that script instead of what is was pointing to before)

提交回复
热议问题