2016-09-30 49 views
-1

這裏是進程替換的玩具的例子,在Bash中正常工作:Python的子進程不允許使用shell = True進行進程替換嗎?

$ wc -l <(pwd) 
1 /proc/self/fd/11 

那麼,爲什麼同樣的命令給從Python的子帶外殼時調用語法錯誤=真?

>>> subprocess.check_call('wc -l <(pwd)', shell=True) 
/bin/sh: 1: Syntax error: "(" unexpected 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/path/to/my/python/lib/python3.5/subprocess.py", line 581, in check_call 
    raise CalledProcessError(retcode, cmd) 
subprocess.CalledProcessError: Command 'wc -l <(pwd)' returned non-zero exit status 2 
+0

'shell = True'不使用Bash。它使用'/ bin/sh'。 (我不確定這是否可配置。) – user2357112

+0

是的,這是問題所在。謝謝! –

+0

另請參閱https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash究竟是什麼區別。 – tripleee

回答

2

/bin/sh: 1: Syntax error: "(" unexpected

你有一個bashism。根據POSIX,這是無效的,這是/bin/sh實現的。

+2

謝謝!解決方法是用'executable ='/ bin/bash''調用'check_call'。 –

2

另一種解決方案是將更多的shell代碼轉移到Python本身。例如:

from subprocess import Popen, PIPE, check_call 

p1 = Popen(["pwd"], stdout=PIPE) 
p2 = check_call(["wc", "-l"], stdin=p1.stdout) 

這往往可以實現消除需要使用subprocess在所有的第一步,因爲它分解工作成小塊,而您可能更容易地看到如何在Python本身做。