1

I using a Python script to call a .sh script, as shown below

ret = subprocess.call('sh ./myScript.sh '+file_a+' '+file_b+' '+str(self.counter), shell=True)

The first two lines of myScript.sh are

#!/bin/bash
echo "sh script opened" 

I can run myScript.sh directly from Windows Powershell, but not from my Python script(run in Powershell). So, I know that myScript.sh is correct; however, I do not get output when run from my Python script On a different computer, this call worked fine, but now it doesn't. Any suggestions?

1
  • What's your COMSPEC environment variable? If it's cmd (or unset) rather than powershell, Python will run the command in a cmd subshell, which would explain why it doesn't give the same results as running the same command in powershell. Commented Oct 8, 2013 at 21:07

1 Answer 1

1

As @abarnet mentioned in his comment there is a COMSPEC environement variable that you can change to point at powershell rather than cmd:

import os
import subprocess

print os.environ["COMSPEC"]
os.environ["COMSPEC"] = r"C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe"
print os.environ["COMSPEC"]
ret = subprocess.call('sh ./script.sh', shell=True)
print ret

Which on my Windows machine returns:

C:\Windows\system32\cmd.exe 
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe 
The term 'sh' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included , verify that the path is correct and try again. At line:1 char:3
+ sh <<<<  ./script.sh
    + CategoryInfo          : ObjectNotFound: (sh:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Sign up to request clarification or add additional context in comments.

Comments

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.