bash: choose default from case when enter is pressed in a “select” prompt

后端 未结 4 1401
说谎
说谎 2021-02-05 16:55

I\'m prompting questions in a bash script like this:

optionsAudits=(\"Yep\" \"Nope\")
    echo \"Include audits?\"
    select opt in \"${optionsAudits[@]}\"; do
         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-05 17:37

    This will do what you are asking for.

    options=("option 1" "option 2");
    while :
    do
        echo "Select your option:"
        i=1;
        for opt in  "${options[@]}"; do
            echo "$i) $opt";
            let i++;
        done
    
        read reply
        case $reply in
            "1"|"${options[0]}"|"")
              doSomething1();
              break;;
            "2"|"${options[1]}")
              doSomething2();
              break;;
            *)
              echo "Invalid choice. Please choose 1 or 2";;
        esac
    done
    

提交回复
热议问题