3

I want to execute a shell script, on cygwin from Python. The shell script is creating a file as an output.

I tried

import os
import subprocess

os.chdir(r"C:\\cygwin64\\bin\\ ")
cmd = ["bash", "-c", 'cd /<path for the script>; ./test.sh']
subprocess.call(cmd)
5
  • 1
    os.chdir(r"C:\\cygwin64\\bin\\ ") contains a space. What is your error? Commented Dec 13, 2017 at 22:05
  • are you using the python of cygwin or the windows one? Commented Dec 14, 2017 at 0:36
  • it gives error as grep command not found, i am using grep in the first line of the shell script Commented Dec 14, 2017 at 10:56
  • no i am using python of windows. Commented Dec 14, 2017 at 11:01
  • Have you solved this issue? I have a script and some commands work and some others like "cp", "sed" doesn't Commented Feb 3, 2024 at 22:18

1 Answer 1

4

This works:

import os, subprocess
os.chdir(r"C:\cygwin64\bin")
cmd = ["bash", "-c", "cd $HOME; pwd; exit"]
ret = subprocess.call(cmd)
Sign up to request clarification or add additional context in comments.

2 Comments

A description, why this solves the problem, greatly improves your answer. Please read How do I write a good answer?
I have tried with a script that includes commands like cp or sed and it doesn't work. But there are other commands in the same script, like test, do, for, echo and works fine. Any idea?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.