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 Stack Overflow to learn, share knowledge, and build your career.
At least five answers for one generic question.Depending on
and if you want
1. POSIX generic solutionsYou could use the
POSIX, but single key featureBut if you don't want the user to have to hit Return, you could write: (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 are many tools which were built using
Depending on your system, you may need to replace
where
2. Bash specific solutionsBasic in line method
I prefer to use in line with single key featureUnder bash, we can specify the length of intended input for for the
Under bash,
Some tricks for dedicated toolsMore sophisticated dialog boxes, beyond simple
Progress bar:
Little demo:
5. Using readline's historyExample:
This will create a file |
|||||||||||||||||||||
|
|
With php, this is powerful for shell scripts:
The php-cli package only is mandatory for this. |
|||||||||||||||||
|
Confirm with clean user inputFunction:
Usage:
|
|||
|
|
|
The simplest and most widely available method to get user input at a shell prompt is the
Another method, pointed out by Steven Huwig, is Bash's
With Also, please check out the excellent answer by F. Hauri. |
|||||||||||||||||||||
|
|
Use the
and then all of the other stuff you need |
||||
|
|
|
The easiest way to achieve this with the least number of lines is as follows:
The |
||||
|
|
|
I noticed that no one posted an answer showing multi-line echo menu for such simple user input so here is my go at it:
This method was posted in the hopes that someone may find it useful and time-saving. |
|||
|
|
|
|||||||||||||||||
|
|
I've used the Example of using a
|
||||
|
|
You want:
Snippet
Explanations
Timeout => Default answer is No
|
|||||||||
|
|
more generic would be:
|
|||
|
|
|
You can use the built-in read command. Use the Since BASH4, you can now use
(But remember to use the "readline" option |
|||||
|
|
As a friend of a one line command I used the following:
Written longform, it works like this:
|
|||||||||||||
|
The If you want to use a suggestion as input:
|
|||||||||||||
|
|
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.,
|
|||||||||
|
|
Inspired by the answers of @Mark and @Myrddin I created this function for a universal prompt
use it like this:
|
|||||
|
|
|||||||||||||
|
|
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. |
|||||
|
|
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. |
|||
|
|
|
Multiple choice version:
Example:
It will set |
|||
|
|
|
This solution reads a single character and calls a function on a yes response.
|
|||||||||
|
|
|||||||||
|
|
Here's something I put together:
I'm a beginner, so take this with a grain of salt, but it seems to work. |
|||||
|
|
|||
|
|
|
Bash has select for this purpose.
|
|||||||||||||||||
|
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?