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

后端 未结 30 2113
不思量自难忘°
不思量自难忘° 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:21

    more generic would be:

    function menu(){
        title="Question time"
        prompt="Select:"
        options=("Yes" "No" "Maybe")
        echo "$title"
        PS3="$prompt"
        select opt in "${options[@]}" "Quit/Cancel"; do
            case "$REPLY" in
                1 ) echo "You picked $opt which is option $REPLY";;
                2 ) echo "You picked $opt which is option $REPLY";;
                3 ) echo "You picked $opt which is option $REPLY";;
                $(( ${#options[@]}+1 )) ) clear; echo "Goodbye!"; exit;;
                *) echo "Invalid option. Try another one.";continue;;
             esac
         done
         return
    }
    

提交回复
热议问题