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 in a typical bash prompt?
Join them; it only takes a minute:
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. Also, please check out the excellent answer by F. Hauri below. |
|||||||||||||||||||||
|
|
|||||
|
At least five answer for one generic question.Edit 2015-10-29: Adding readline's history capacityDepending on
and if you want
1. Posix generic solutionsWith poor language features, you could use
Posix, but single key featureBut if you want user not to have to hit (Edited: As @JonathanLeffler rightly suggest, saving stty's configuration could be better than simply force them to sane.)
Note: This was tested under sh, bash, ksh, dash and busybox! Same, but waiting explicitly for y or n:
Using dedicated toolsThere is a bunch of tools which were built, using 'libncurses, libgtk, libqt' or any graphical library, for this kind of goal:
Depending on which distribution you're using, you could replace
Where
2. Bash specific solutionsBasic in line method
I prefer to use in line with single key featureFor this, under bash, we just have to specify the length of attended input for
Under bash,
Using dedicated toolsOf course, all graphical tools work same under bash:
3. Some tricks about dedicated toolsIf
Storing answer into a variable is something tricky: The standard output is for interface drawing and the answer is printed on the error output:
or under bash:
or
Progress bar:
Little demo:
5. Using readline's historyMore than words, look (or try) this sample:
This will create a file |
|||||||||||||
|
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. |
|||||||||
|
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. |
|||||||||||||||||
|
The If you want to use a suggestion as input:
|
|||||||||||||
|
The easiest way to achieve this with the least number of lines is as follows:
The "if" is just an example, it is up to you how to handle this variable. |
||||
|
Use the
and then all of the other stuff you need |
|||||||||||||
|
You want:
Snippet
Explanations
Timeout => Default answer is No
|
|||||
|
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. |
|||||
|
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. |
|||
|
One simple way to do this is with I like the behavior of xargs a little better for this because it executes each command immediately after the prompt like other interactive unix commands, rather than collecting the yesses to run at the end. (You can Ctrl-C after you get through the ones you wanted.) e.g.,
|
|||||||||
|
Multiple choice version:
Example:
It will set |
|||
|
As a friend of a one line command I used the following:
Written longform, it works like this:
|
|||||||||
|
|
|||||
|
Inspired by the answers of @Mark and @Myrddin I created this function for a universal prompt
use it like this:
|
|||||
|
more generic would be:
|
|||
|
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 or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?