如何填写命令行界面?

How to fill in a Command Line Interface?

我要自动化遗留系统的安装阶段,因为我不想在安装它时一次又一次地付出更多的努力。在 Linux Terminal 的安装过程中,我必须回答一些关于基本配置的问题。实际上,将 shell 命令全部放入一个批处理文件中很容易实现自动化,如下所示:

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

但是,我不知道如何自动回答如下特定问题:

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

有没有什么工具或者编程语言可以处理这种情况?或者有没有办法将答案作为批处理文件中的参数传递?

提前致谢。

所以,假设这是脚本的简化版本:

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

这是在一个名为 answers

的文件中
thingywhatnot
11.12.33.240
bozo@brains.com

你会运行

installationScript < answers

它会打印这个

Received thingywhatnot, 11.12.33.240, bozo@brains.com

这项工作的经典 Linux 工具是 expect

expect 中,您可以期待不同的问题和问题的变体,并且不必准确输入问题。 expect 不会盲目回答每一个提示,而是针对实际提出的问题提供答案。

这是一个简短的例子:

#!/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 "foo@bar.com\r"
expect "Would you like to disable UDP services?(y/n) [n]:"
send "y\r"