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
Post a Comment