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!)

6
  • @user3140225 The linked duplicate question in the preceding comment is not a duplicate of this question.
    – karel
    Commented Oct 15, 2020 at 19:44
  • 1
    Whatever is prompting you appears to be reading directly from the terminal, not standard input.
    – chepner
    Commented Oct 15, 2020 at 20:31
  • 3
    @karel Could you please tell me why do you think that? The linked question also has three answers that use expect (one of them is the accepted answer). Also, in both questions the users want the same thing: use a script to enter input in multiple prompts. The only difference is stated in the body (not in the title) of the linked question, which is that the user wants to enter "yes" to every prompt, but in my opinion doesn't suffice to making it not a duplicate. Commented Oct 15, 2020 at 20:45
  • @user3140225 That's a good explanation of why you think it's a duplicate question, but having two questions (one for each use case) instead of one question for both use cases makes both use cases more easily searchable on Ask Ubuntu instead of just the "yes" prompt use case. Answers to simple questions about bash commands are sometimes impossible to search for as it is (That's why the accepted answer to this question was upvoted so much.), so I nominate this one to have two questions instead of one.
    – karel
    Commented Oct 15, 2020 at 20:55
  • 2
    @karel - that is the purpose of the 'close as duplicate' flag - the question remains searchable, and links to the other question
    – dcorking
    Commented Oct 16, 2020 at 10:53

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .