formateu
formateu

Reputation: 167

How to run program in cygwin using python script from windows?

I want to run a program in cygwin through command using python script from windows level and pass parameters for stdin. I read through many topics on stackoverflow already, but solutions which I have found don't work for me. Here is code based on stack topics:

from subprocess import Popen, PIPE

cygwin = Popen(['CYGWINPATH\\bash.exe', '-'],stdin=PIPE,stdout=PIPE)
cygwin.communicate(input="commandToRun")

This doesn't find proper command:

/usr/bin/bash: line 1: uname: command not found
('', None)

EDIT : Credit to matzeri for suggesting bash instead of mintty. Example: I have a python script on windows desktop and after doubleclick I want to open program inside of cygwin and pass parameters for stdin.

Upvotes: 1

Views: 3347

Answers (1)

matzeri
matzeri

Reputation: 8496

mintty is not a command shell, you should use bash instead.

$ cat prova.py
#!/usr/bin/python
from subprocess import Popen, PIPE

cygwin = Popen(['bash'],stdin=PIPE,stdout=PIPE)
result=cygwin.communicate(input="uname -a")
print result

so you can have

$ ./prova.py
    ('CYGWIN_NT-6.1 GE-MATZERI-EU 2.5.2(0.297/5/3) 2016-06-23 14:29 x86_64 Cygwin\n', None)

Second example with a stand alone program

#!/usr/bin/python
from subprocess import Popen, PIPE

cygwin = Popen(['lftp'],stdin=PIPE,stdout=PIPE)
result=cygwin.communicate(
input=" set dns:order inet inet6 \n\
open ftp.mirrorservice.org/sites/sourceware.org/pub/cygwin/x86/release\n\
find lftp\n\
quit ")
print result[0]

and the output is

$ ./prova2.py
lftp/
lftp/lftp-4.6.5-1-src.tar.xz
lftp/lftp-4.6.5-1.tar.xz
lftp/lftp-4.7.2-1-src.tar.xz
lftp/lftp-4.7.2-1.tar.xz
lftp/lftp-debuginfo/
lftp/lftp-debuginfo/lftp-debuginfo-4.6.5-1.tar.xz
lftp/lftp-debuginfo/lftp-debuginfo-4.7.2-1.tar.xz
lftp/lftp-debuginfo/md5.sum
lftp/lftp-debuginfo/setup.hint
lftp/lftp-debuginfo/sha512.sum
lftp/md5.sum
lftp/setup.hint
lftp/sha512.sum

Upvotes: 2

Related Questions