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

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

    You can use the default REPLY on a read, convert to lowercase and compare to a set of variables with an expression.
    The script also supports ja/si/oui

    read -rp "Do you want a demo? [y/n/c] "
    
    [[ ${REPLY,,} =~ ^(c|cancel)$ ]] && { echo "Selected Cancel"; exit 1; }
    
    if [[ ${REPLY,,} =~ ^(y|yes|j|ja|s|si|o|oui)$ ]]; then
       echo "Positive"
    fi
    

提交回复
热议问题