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

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

    Try,

     #!/bin/bash
     pause ()
     {
     REPLY=Y
     while [ "$REPLY" == "Y" ] || [ "$REPLY" != "y" ]
     do
      echo -e "\t\tPress 'y' to continue\t\t\tPress 'n' to quit"
      read -n1 -s
          case "$REPLY" in
          "n")  exit                      ;;
          "N")  echo "case sensitive!!"   ;; 
          "y")  clear                     ;;
          "Y")  echo "case sensitive!!"   ;;
          * )  echo "$REPLY is Invalid Option"     ;;
     esac
     done
     }
     pause
     echo "Hi"
    

提交回复
热议问题