如何填写命令行界面?

时间:2015-06-09 10:18:12

标签: linux command-line-interface

我要自动化遗留系统的安装阶段,因为我不希望在我想安装它时一次又一次地付出更多努力。在Linux终端上的安装过程中,我必须回答一些有关基本配置的问题。实际上,通过将它们全部放在如下所示的批处理文件中来自动化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]:
....

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

提前致谢。

2 个答案:

答案 0 :(得分:4)

这项工作的经典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"

答案 1 :(得分:1)

所以,想象一下这是脚本的简化版本:

#!/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