1

Hey i'm trying to run a shell Script with python using the Following lines:

import subprocess

shellscript = subprocess.Popen(["displaySoftware.sh"], stdin=subprocess.PIPE)

shellscript.stdin.write("yes\n")
shellscript.stdin.close()
returncode = shellscript.wait()

But when I run the Program it says that it can't find the .sh file.

enter image description here

4
  • Can you post the complete traceback? Commented Sep 12, 2021 at 15:44
  • Try giving it the complete path of the .sh file. Maybe the "current path" where you're running the script is not the same as the path where the script is. Commented Sep 12, 2021 at 16:06
  • 1
    @Teer2008, if your displaySoftware.sh has executable permissions and starts with a valid shebang, you can just change ["displaySoftware.sh"] to ["./displaySoftware.sh"], adding a leading ./, and that's all you need to do -- no shell=True, no sh. And it works better that way, because it honors your script's shebang to select the interpreter to use. Commented Sep 12, 2021 at 18:39
  • @Teer2008, ...mind, it would be better instead of hardcoding ./ to refer to the __file__ attribute of your module to find the directory with your Python source code -- that way your script will still work if it's run from a different directory than the one with the source (which is a bug you still have with the accepted answer). Commented Sep 12, 2021 at 18:41

1 Answer 1

2

Your command is missing "sh", you have to pass "shell=True" and "yes\n" has to be encoded.

Your sample code should look like this:

import subprocess

shellscript = subprocess.Popen(["sh displaySoftware.sh"], shell=True, stdin=subprocess.PIPE )

shellscript.stdin.write('yes\n'.encode("utf-8"))
shellscript.stdin.close()
returncode = shellscript.wait()

This method might be better:

import subprocess

shellscript = subprocess.Popen(["displaySoftware.sh"], shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
returncode = shellscript.communicate(input='yes\n'.encode())[0]
print(returncode)

When running this on my machine the "displaySoftware.sh" script, that is in the same directory as the python script, is successfully executed.

Sign up to request clarification or add additional context in comments.

4 Comments

This is very inefficient. When you do this, you're telling Python to start two shells when it only needs one.
That is to say: shell=True prepends ['sh', '-c'] to the given command list, so you're running sh -c 'sh displaySoftware.sh'. You wouldn't ever do that on a command line, would you?
Moreover, sh displaySoftware.sh is itself bad practice, because it ignores displaySoftware.sh's choice of shells to use and forces sh to be used to execute it, even if it's written for bash, or ksh, or zsh, or so forth.
I updated my answer. Does the second option fix the issues you pointed out Charles Duffy?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.