Respond to Prompt in Bash Script

By  on  

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?

Recent Features

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

  • By
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

Incredible Demos

  • By
    The Simple Intro to SVG Animation

    This article serves as a first step toward mastering SVG element animation. Included within are links to key resources for diving deeper, so bookmark this page and refer back to it throughout your journey toward SVG mastery. An SVG element is a special type of DOM element...

  • By
    Digg-Style Dynamic Share Widget Using MooTools

    I've always seen Digg as a very progressive website. Digg uses experimental, ajaxified methods for comments and mission-critical functions. One nice touch Digg has added to their website is their hover share widget. Here's how to implement that functionality on your site...

Discussion

  1. As far as I know this is usually done with the yes command:

    yes | docker-foo bar

    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:

      cat responses.txt | docker-foo bar
  2. MaxArti

    Can’t you stream a file in? Like in

    some-command < my-answers.txt
  3. Daniel

    Try a here string.

    while IFS= read -r; do
        echo "E- $REPLY"
    done <<<"y
    n
    3"
    

    Will print the three lines of responses. Assuming the docker prompt is similar to read it should work.

  4. Rohit

    The ‘expect’ utility can be installed on most Linux boxes.

    https://en.wikipedia.org/wiki/Expect

  5. Mike Wilson

    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 ;-)

  6. David Viklund

    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

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!