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


i want pause input in shell script, , prompt user choices. standard 'yes, no, or cancel' type question. how accomplish in typical bash prompt?

the simplest , available method user input @ shell prompt read command. best way illustrate use simple demonstration:

while true;     read -p "do wish install program?" yn     case $yn in         [yy]* ) make install; break;;         [nn]* ) exit;;         * ) echo "please answer yes or no.";;     esac done 

another method, pointed out steven huwig, bash's select command. here same example using select:

echo "do wish install program?" select yn in "yes" "no";     case $yn in         yes ) make install; break;;         no ) exit;;     esac done 

with select don't need sanitize input – displays available choices, , type number corresponding choice. loops automatically, there's no need while true loop retry if give invalid input.

also, please check out excellent answer f. hauri.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -