3

I'm making a script and my app.py looks like this;

import subprocess

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

And you might already know that this apt-gets are asking for a confirmation. I know I can put an extra -y at the end but I need to know how to make it so it presses enter when prompted.

There are several prompts that doesn't accept -y so that's why I'm asking.

For example; adding a repository or this screen when you run apt-get upgrade on Ubuntu 18.04

So I need to press Enter automatically for these prompts. Which should be something like in this concept;

import subprocess

subprocess.call(["sudo", "apt-get", "update"])
(PRESS ENTER HERE WHEN PROMPTED)
subprocess.call(["sudo", "apt-get", "upgrade"])
(PRESS ENTER HERE WHEN PROMPTED WHICH ASKS YOU ABOUT 3 TIMES)
  • Have you tried `--force-yes` ? – gogaz Aug 19 '18 at 10:13
  • how do I do that exactly? – aradabir007 Aug 19 '18 at 10:14
  • If they are reading from `stdin`, the simplest way is to put each "enter" as a newline in a text file, along with any other replies, and redirect stdin from the file. For example: `subprocess.call("sudo apt-get update < cmd.txt", shell = True)` – cdarke Aug 19 '18 at 10:14
  • Actually the code I pasted is exactly what my app.py is. So I'm not using anything else like stdin or something like that since I don't know how to use them. – aradabir007 Aug 19 '18 at 10:15
  • I meant if `apt-get` is reading from `stdin` – cdarke Aug 19 '18 at 10:16
  • 1
    Why do you even use pyton if you are just using it to run subprocess? You can do that more easily in a shell script. – zvone Aug 19 '18 at 10:17
  • I thought python would be a better fit. Obviously I can do it with a shell script tho. – aradabir007 Aug 19 '18 at 10:21
  • @zvone besides, is it possible to do what I'm asking with shell script? I mean is it easier to make shell script to press enter when prompted? – aradabir007 Aug 19 '18 at 10:22
  • @aradabir007 please check my answer; I think it may be of use for you. –  Aug 19 '18 at 11:46
  • @zvone it looks like so; however the OP has asked for a Python implementation and I don't think arguing their question is going to do much to it. I encourage you to post an answer if you think you can do it more easily –  Aug 19 '18 at 11:50

0 Answers0