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

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

提交回复
热议问题