Ubuntu – Installation of a package with parameters being prompted on cli

automationcommand linedpkgsoftware installation

Say I need to install a package using dpkg -i, which would prompt user for some values such as username and password.

I can easily do it from terminal and proceed with installation.

But going by this plan I cannot automate the installation, for example if I want to write a script for installation of a package which takes the parameters interactively.

So I want a dpkg installation plan which is interactive (prompting for username and password) but can still be able to be invoked from a script so that the installation process is automated.

How can I go about this? Are there any alternatives?

Best Answer

For such things you can write an expect script. It's not very difficult to handle.

First you have to install the interpreter:

apt-get install expect

Then you can write something like this for example:

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

The script must be executable, of course. Expect is perfect for controlling interactive terminal programs via script (ssh, ftp, ...)

Related Question