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

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

    This version allows you to have more than one case y or Y, n or N

    1. Optionally: Repeat the question until an approve question is provided

    2. Optionally: Ignore any other answer

    3. Optionally: Exit the terminal if you want

      confirm() {
          echo -n "Continue? y or n? "
          read REPLY
          case $REPLY in
          [Yy]) echo 'yup y' ;; # you can change what you do here for instance
          [Nn]) break ;;        # exit case statement gracefully
          # Here are a few optional options to choose between
          # Any other answer:
      
          # 1. Repeat the question
          *) confirm ;;
      
          # 2. ignore
          # *) ;;
      
          # 3. Exit terminal
          # *) exit ;;
      
          esac
          # REPLY=''
      }
      

    Notice this too: On the last line of this function clear the REPLY variable. Otherwise if you echo $REPLY you will see it is still set until you open or close your terminal or set it again.

    0 讨论(0)
  • 2020-11-29 15:36

    Well, here's my version of confirm, modified from James' one:

    function confirm() {
      local response msg="${1:-Are you sure} (y/[n])? "; shift
      read -r $* -p "$msg" response || echo
      case "$response" in
      [yY][eE][sS]|[yY]) return 0 ;;
      *) return 1 ;;
      esac
    }
    

    These changes are:

    1. use local to prevent variable names from colliding
    2. read use $2 $3 ... to control its action, so you may use -n and -t
    3. if read exits unsuccessfully, echo a line feed for beauty
    4. my Git on Windows only has bash-3.1 and has no true or false, so use return instead. Of course, this is also compatible with bash-4.4 (the current one in Git for Windows).
    5. use IPython-style "(y/[n])" to clearly indicate that "n" is the default.
    0 讨论(0)
  • 2020-11-29 15:37

    Late to the game, but I created yet another variant of the confirm functions of previous answers:

    confirm ()
    {
        read -r -p "$(echo $@) ? [y/N] " YESNO
    
        if [ "$YESNO" != "y" ]; then
            echo >&2 "Aborting"
            exit 1
        fi
    
        CMD="$1"
        shift
    
        while [ -n "$1" ]; do
            echo -en "$1\0"
            shift
        done | xargs -0 "$CMD" || exit $?
    }
    

    To use it:

    confirm your_command
    

    Features:

    • prints your command as part of the prompt
    • passes arguments through using the NULL delimiter
    • preserves your command's exit state

    Bugs:

    • echo -en works with bash but might fail in your shell
    • might fail if arguments interfere with echo or xargs
    • a zillion other bugs because shell scripting is hard
    0 讨论(0)
  • 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? <y/N> " 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.

    0 讨论(0)
  • 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"
    
    0 讨论(0)
提交回复
热议问题