Automating Responses with expect
There may be times when you’ll need to run a script that stops multiple times to prompt you for some sort of input. That can get a bit tedious after a while, especially if you need to run the script multiple times on multiple servers or workstations. Wouldn’t it be nice to automate the responses? Well, with all due respect, you can with expect
.
So, what is expect
? Well, it’s a programming environment, similar to what you have with bash
. If you have an interactive shell script that expects certain responses, you can use expect
to automatically send the correct responses. Let’s begin with the simplest of examples. First, create the interactive_script.sh
script, like so:
#!/bin/bash
echo "Hello. What is your name?"
read $reply
echo "What's your favorite operating system?"
read $reply
echo "How many cats to you have?"
read $reply
echo "How many dogs do you have?"
read $reply...