我想使用子shell的魔力和python子进程模块的重定向,但它似乎不起作用,抱怨意外的令牌是括号。例如,命令
cat <(head tmp)
传递给子进程时给出了这个
>>> subprocess.Popen("cat <(head tmp)", shell=True)
<subprocess.Popen object at 0x2b9bfef30350>
>>> /bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `cat <(head tmp)'
<(head tmp)
语法是一种称为“进程替换”的bash
特性。基本/便携式/bin/sh
不支持它。 (即使在/bin/sh
和/bin/bash
是相同程序的系统上也是如此;当调用普通/bin/sh
时它不允许使用此功能,因此您不会无意中依赖于非便携式功能。)
>>> subprocess.Popen(["/bin/bash", "-c", "cat <(head tmp)"])
<subprocess.Popen object at 0x1004cca50>