在cli上安装带有参数的软件包

假设我需要使用dpkg -i安装软件包,这会提示用户输入一些值,例如用户名和密码。

我可以从终端轻松完成并继续安装。

但是按照这个计划我不能自动安装,例如,如果我想编写一个脚本来安装一个以交互方式获取参数的包。

所以我想要一个交互式dpkg安装计划(提示输入用户名和密码),但仍然可以从脚本调用,以便安装过程自动化。

我怎么能这样做? 还有其他选择吗?

对于这样的事情,你可以编写一个expect脚本。 这不是很难处理。

首先,您必须安装解释器:

 apt-get install expect 

然后你可以写这样的东西,例如:

 #!/usr/bin/expect -f set timeout 30 set password "pass" set username "user" #run the command spawn dpkg -i package.deb # Look for username prompt expect "*?sername:*" #<--- this statement is important it wait's for a prompt "username:" send "$username\r" # Look for passwod prompt expect "*?assword:*" #<--- the same with the "password:" prompt send "$password\r" #dpkg -i continues 

当然,脚本必须是可执行的。 Expect非常适合通过脚本控制交互式终端程序(ssh,ftp,...)

您可以使用debconf-utils包中的工具创建预配置文件。 您可以使用以下方法手动创建预配置文件:

     

或作为一个例子:

 my-package username string Bob my-package password string I$aN1ceGuy 

更容易将其安装到您的机器上并运行:

 debconf-get-selections | grep my-package 

使用输出创建文件。

获得文件后,使用:

 debconf-set-selections . dpkg -i my-package 

默认情况下将使用filename中列出的选项,程序包将以静默方式安装。

http://www.debian.org/releases/stable/i386/apbs03.html.en