簡體   English   中英

如何使用 Windows 中的 python 腳本在 cygwin 中運行程序?

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

我想通過使用 Windows 級別的 python 腳本的命令在 cygwin 中運行一個程序,並為 stdin 傳遞參數。 我已經閱讀了許多關於 stackoverflow 的主題,但我發現的解決方案對我不起作用。 這是基於堆棧主題的代碼:

from subprocess import Popen, PIPE

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

這沒有找到正確的命令:

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

編輯:感謝 matzeri 建議使用 bash 而不是 mintty。 示例:我在 Windows 桌面上有一個 python 腳本,雙擊后我想在 cygwin 中打開程序並為 stdin 傳遞參數。

mintty 不是命令外殼,您應該使用 bash 代替。

$ 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

所以你可以擁有

$ ./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)

帶有獨立程序的第二個示例

#!/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]

輸出是

$ ./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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM