Search code examples
pythonsubprocess

Shell script as file with output to python


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.


Solution

  • 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.