How do I prompt for Yes/No/Cancel input in a Linux shell script?

后端 未结 30 1699
不思量自难忘°
不思量自难忘° 2020-11-22 04:52

I want to pause input in a shell script, and prompt the user for choices.
The standard Yes, No, or Cancel type question.
How d

相关标签:
30条回答
  • 2020-11-22 05:11

    The easiest way to achieve this with the least number of lines is as follows:

    read -p "<Your Friendly Message here> : y/n/cancel" CONDITION;
    
    if [ "$CONDITION" == "y" ]; then
       # do something here!
    fi
    

    The if is just an example: it is up to you how to handle this variable.

    0 讨论(0)
  • 2020-11-22 05:14
    inquire ()  {
      echo  -n "$1 [y/n]? "
      read answer
      finish="-1"
      while [ "$finish" = '-1' ]
      do
        finish="1"
        if [ "$answer" = '' ];
        then
          answer=""
        else
          case $answer in
            y | Y | yes | YES ) answer="y";;
            n | N | no | NO ) answer="n";;
            *) finish="-1";
               echo -n 'Invalid response -- please reenter:';
               read answer;;
           esac
        fi
      done
    }
    
    ... other stuff
    
    inquire "Install now?"
    
    ...
    
    0 讨论(0)
  • 2020-11-22 05:15
    echo "Please enter some input: "
    read input_variable
    echo "You entered: $input_variable"
    
    0 讨论(0)
  • 2020-11-22 05:15

    Multiple choice version:

    ask () {                        # $1=question $2=options
        # set REPLY
        # options: x=..|y=..
        while $(true); do
            printf '%s [%s] ' "$1" "$2"
            stty cbreak
            REPLY=$(dd if=/dev/tty bs=1 count=1 2> /dev/null)
            stty -cbreak
            test "$REPLY" != "$(printf '\n')" && printf '\n'
            (
                IFS='|'
                for o in $2; do
                    if [ "$REPLY" = "${o%%=*}" ]; then
                        printf '\n'
                        break
                    fi
                done
            ) | grep ^ > /dev/null && return
        done
    }
    

    Example:

    $ ask 'continue?' 'y=yes|n=no|m=maybe'
    continue? [y=yes|n=no|m=maybe] g
    continue? [y=yes|n=no|m=maybe] k
    continue? [y=yes|n=no|m=maybe] y
    $
    

    It will set REPLY to y (inside the script).

    0 讨论(0)
  • 2020-11-22 05:16

    Inspired by the answers of @Mark and @Myrddin I created this function for a universal prompt

    uniprompt(){
        while true; do
            echo -e "$1\c"
            read opt
            array=($2)
            case "${array[@]}" in  *"$opt"*) eval "$3=$opt";return 0;; esac
            echo -e "$opt is not a correct value\n"
        done
    }
    

    use it like this:

    unipromtp "Select an option: (a)-Do one (x)->Do two (f)->Do three : " "a x f" selection
    echo "$selection"
    
    0 讨论(0)
  • 2020-11-22 05:17
    read -p "Are you alright? (y/n) " RESP
    if [ "$RESP" = "y" ]; then
      echo "Glad to hear it"
    else
      echo "You need more bash programming"
    fi
    
    0 讨论(0)
提交回复
热议问题