Respond to Prompt in Bash Script
I work on a project that requires I frequently build and destroy a virtual machine. I don't enjoy having to do so but virtual machines can be notoriously difficult to prop up, especially when you have a complex app living within it.
Manually typing the same commands over and over can be mind-numbing so I've create a bash script to handle all of the work for me. One hiccup to the process is needing to confirm removal of an existing virtual machine; using a pipe and echo
allows me to answer the prompt:
# ... some directives here
# Remove the machine, confirming "y" when asked by docker-machine
echo 'y' | docker-machine rm default
# ... more directives here
Using echo
I pipe a y
response to docker-machine
's confirmation prompt, thus allowing the script to move forward with other tasks without the need for manual intervention.
I don't, however, know how to handle multiple prompts -- can you tell me and everyone else?
![Create a CSS Flipping Animation]()
CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...
![CSS vs. JS Animation: Which is Faster?]()
How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps?
This article serves as a point-by-point...
![MooTools & Printing – Creating a Links Table of Contents]()
One detail we sometimes forget when considering print for websites is that the user cannot see the URLs of links when the page prints. While showing link URLs isn't always important, some websites could greatly benefit from doing so. This tutorial will show you...
![Create Classy Inputs Using MooTools’ OverText]()
The MooTools More library is a goldmine. A treasure chest. Pirates booty, if you will (and, of course, I will). More is full of plugins that add a lot of class and functionality to your website with minimal effort.
As far as I know this is usually done with the yes command:
This should also respond to multiple prompts.
Ah yes, I did experiment with that. But what if I know the multiple prompt responses should be “y”, “n”, “3”? That would be useful.
To expand on what Andreas said, you can build a canned file of responses, one per line (extra blanks for the equivalent of hitting enter) and sending that to your program:
Can’t you stream a file in? Like in
Try a here string.
Will print the three lines of responses. Assuming the docker prompt is similar to read it should work.
The ‘expect’ utility can be installed on most Linux boxes.
https://en.wikipedia.org/wiki/Expect
I agree about ‘expect’ as Rohit mentions, it’s usually what you resort to when piping doesn’t work for different reasons (such as passwords being read from physical terminal only, input being prematurely consumed by a loop, etc).
The tool is {advanced|complicated} enough to maybe warrant a future post ;-)
In my team we use Vagrant and Ansible to do these kinds of things. They are very helpful tool for popping up a dev env with customized apps / configs.
I have not tried Ansible for Docker but there is a page for it https://www.ansible.com/docker