How to fill in a Command Line Interface?

263 views Asked by At

I am to automate the installation phase of a legacy system, because I do not want to put more efforts again and again when ever I want to install it. During the installation process on Linux Terminal, I have to answer some questions regarding the basic configurations. Indeed, it is easy to automate the shell commands by putting them all in a batch file like the following:

bin/startServer destination/sub_v1
bin/startAdminInterface
....

However, I do not know how to automate the answers of a specific questions like the following:

Enter the server's IP address: 
Enter the email of the contact person:
Would you like to disable UDP services?(y/n) [n]:
....

Is there any tool or programming language can deal with this situation? or Is there any way to pass the answers as a parameters within the batch file?

Thanks in advance.

2

There are 2 answers

7
dotancohen On BEST ANSWER

The classic Linux tool for this job is expect.

With expect one can expect different questions and variations on a question, and the question does not have to be typed exactly. expect does not blindly answer every prompt, but rather it provides answers to the questions actually asked.

Here is a short example:

#!/usr/bin/expect -f
spawn someScript.sh
expect "Enter the server's IP address:"
send "10.0.0.4\r"
expect "Enter the email of the contact person:"
send "[email protected]\r"
expect "Would you like to disable UDP services?(y/n) [n]:"
send "y\r"
3
Mark Setchell On

So, imagine this is a simplified version of the script:

#!/bin/bash
read -p "Enter something:" thing
read -p "Enter server IP:" ip
read -p "Enter Email:" email
echo Received $thing, $ip, $email

and this is in a file called answers

thingywhatnot
11.12.33.240
[email protected]

You would run

installationScript < answers

and it would print this

Received thingywhatnot, 11.12.33.240, [email protected]