I am triggering a shell script using a Python script and get its exit code for further processing. I am using the subprocess module. But when I try to capture the exit code for further processing, it comes with the script's output.
How can I only get the exit code?
Below is my code
import subprocess
import os
dpath = "/home/admin/temp"
password = "password"
password_bytes = bytes(password + "\n", encoding="utf-8")
proc = subprocess.Popen(
["sudo", "-S", "sh", os.path.join(dpath, "test1.sh"), "hello"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).communicate(input=password_bytes)
out, err = proc
out = str(out, "utf-8").strip()
if out == "0":
print("success")
else:
print("fail")
# print(type(out))
print(out)
Below is my shell script
cd /home/admin
echo $1
echo $?