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

后端 未结 4 1412
说谎
说谎 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条回答
  •  猫巷女王i
    2021-02-05 17:37

    Updated answer:

    echo "Include audits? 1) Yep, 2) Nope"
    read ans
    case $ans in
        Yep|1  )  echo "yes"; includeAudits=true; v=1 ;;
        Nope|2 )  echo "no"; includeAudits=false; v=2 ;;
        ""     )  echo "default - yes"; includeAudits=true; v=1 ;;
        *      )  echo "Whats that?"; exit ;;
    esac
    

    This accepts either "Yep" or "1" or "enter" to select yes, and accepts "Nope" or "2" for no, and throws away anything else. It also sets v to 1 or 2 depending on whether the user wanted yes or no.

提交回复
热议问题