C
C
Chipchilinka3372021-06-16 11:36:53
linux
Chipchilinka337, 2021-06-16 11:36:53

How to pass string from script to terminal?

I use Python's subprocess library to call system commands from a script. When calling the sudo apt upgrade command on Linux, you must press the "y" button to confirm consent. How can I directly pass the button press "y" from the script?

def package_upgrade():
    print("[+] Upgrading packages...")
    subprocess.call(["sudo", "apt", "upgrade"])

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
Lynn "Coffee Man", 2021-06-16
@Chipchilinka337

I recommend reading the documentation. In particular, apt upgrade has flags that allow you not to ask for confirmation.

subprocess.call(["sudo", "apt", "upgrade", "--yes"])

K
ky0, 2021-06-16
@ky0

I will add to the collection, to other quite correct options, one more, suitable not only for apt: yes | some_command_with_confirmations.

R
res2001, 2021-06-16
@res2001

Write to the stdin of the created process the character 'y' and, if necessary, '\n'. See the subprocess doc for how to get a link to the stdin of a process.

F
Fenrir89, 2021-06-17
@Fenrir89

Add option -y or yes or
--noconfirm (default answer most often means don't change configs)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question