1

I have a simple shell script with input and output to console. I want to get console output and errs to variables in Python, preferably with subprocess.run(). This example is simplified but problem is the same - i'm running protoc compilations over hundreds of protofiles.

# test.sh

echo $1 $2

Script is called in Python, it's working (shell script do the job and it's outputed to console) but i can't get the console output back to Python - nothing is printed. What i've tried:

proc = subprocess.run(["test.sh", "test", "test2"],
                      encoding="utf-8",
                      shell=True,
                      text=True,
                      capture_output=True)

print(proc.stdout)
session = subprocess.Popen(['test.sh', "test", "test2"],stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

stdout, stderr = session.communicate()

print(stdout)
with subprocess.Popen(["test.sh", "test", "test2"],
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE,
                     shell=True) as p:
     for line in p.stdout:
         print(line)

Thank you.

darthbane426
  • 113
  • 8
  • 1
    Typo: don't use `shell=True` when your first argument is not a string. Works fine with `text=True` instead; https://ideone.com/TJBOuw – tripleee May 05 '23 at 11:09
  • @tripleee When i don't use it i get error msg with "OSError: [WinError 193] %1 is not a valid Win32 application" – darthbane426 May 05 '23 at 11:12
  • 2
    If you are on Windows, you will need to explicitly pass in `executable=/path/to/bin/bash` or some such; `shell=True` on Windows means "use CMD" which of course does not understand `sh` syntax. Still, the fact remains that `shell=True` does not do at all what you hope here. See also [Actual meaning of `shell=True` in subprocess](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) – tripleee May 05 '23 at 11:13

1 Answers1

1

Try to specify sh/bash explicitly in the list of subprocess.run(*popenargs, ...) and remove shell=True parameter:

proc = subprocess.run(
    ["sh", "./test.sh", "test", "test2"],
    encoding="utf-8",
    text=True,
    capture_output=True
)

print(proc.stdout)
print(proc.stderr)

Also, make sure you print(proc.stderr) to check if there're any errors in stderr.

Anton Petrov
  • 315
  • 1
  • 3
  • 12