I want to pause input in a shell script, and prompt the user for choices. The standard 'Yes, No, or Cancel' type question. How do I accomplish this at a typical bash prompt?
The simplest and most widely available method to get user input at a shell prompt is the 'read' command. The best way to illustrate its use is a simple demonstration:
Another method, pointed out by Steven Huwig, is bash's 'select' command. Here is the same example using select:
With select you don't need to sanitize the input... it prompts you with your choices, and you type a number corresponding to the choice you want. Select also loops automatically... there's no need for a 'while true' loop to retry if they give invalid input. |
|||||||||||||||||||||
|
|
|||
|
You can use the built-in read command. Use the Since BASH4, you can now use
(But remember to use the "readline" option |
||||
|
Bash has select for this purpose.
|
|||||||||||||
|
|
|||
|
Here's something I put together:
I'm a beginner, so take this with a grain of salt, but it seems to work. |
|||||
|
Hi Christopher,
I used the above code in one of my bash script, even though it says |
|||||||||||||
|
This solution reads a single character and calls a function on a yes response.
|
|||||||||
|
Where'd my formatting go? I guess that shows what happens if you cut and paste from unix text. |
|||||||||
|
Sorry for posting on such an old post. Some weeks ago I was facing a similar problem, in my case I needed a solution which also worked within an online installer-script, eg: Using
Hope this helps someone. |
|||||
|
Use the
and then all of the other stuff you need |
|||||
|
|
|||||
|
Multiple choice version:
Example:
It will set |
|||
|
To get a nice ncurses-like inputbox use the command dialog like this:
The dialog package is installed by default at least with SUSE Linux. |
|||
|
I suggest you use dialog...
it's simple and easy to use, there's also a gnome version called gdialog that takes the exact same parameters, but shows it GUI style on X. |
|||||
|
protected by Yu Hao Sep 24 '13 at 3:30
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?