14

I'm trying to automate the installation of CUDA on an Ubuntu 18.04 VM template as part of the startup script. I'm very new to bash and cloud computing in general. When I run the command to install CUDA, mid-installation I am always prompted to choose a country and a language from a list of options by entering the corresponding ID numbers.

Basically, when I run:

sudo apt-get -y install cuda

I know I'm going to have to manually enter "31" and "1" for the install to complete. As a relative n00b at all this, my question is thus "How do I automate those inputs so I don't have to manually type them in every time I spin up a fresh copy of this VM template?"

My initial approach was this:

printf "31\n1\n" | sudo apt-get -y install cuda

But this does not seem to work the way I expected it to. No input is autofilled.

My end goal is to have everything taken care of in one startup script that I don't have to touch.

I appreciate any and all help, and I apologize if my question has been answered elsewhere (if it has, I've been unable to find it and would greatly appreciate being directed to it!)

1 Answers1

25

Install expect and write an expect script that answers the prompts. This will look something like this (warning - not tested in any way):

#!/usr/bin/expect
spawn sudo apt-get -y install cuda
expect "first prompt:"
send "31\r"
expect "second prompt:"
send "1\r"
wait

where you need to substitute appropriate actual prompts instead of "first prompt:" and "second prompt:".

Look here for more information about expect:

https://www.slashroot.in/expect-command-tutorial-linux-example-usage

http://tcl.tk/man/expect5.31/expect.1.html

You may be also interested in autoexpect, which can generate a script automatically by watching your interactive session:

http://tcl.tk/man/expect5.31/autoexpect.1.html

raj
  • 11,409