Python子进程,子shell和重定向

问题描述 投票:5回答:1

我想使用子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)'
python bash subprocess subshell
1个回答
11
投票

<(head tmp)语法是一种称为“进程替换”的bash特性。基本/便携式/bin/sh不支持它。 (即使在/bin/sh/bin/bash是相同程序的系统上也是如此;当调用普通/bin/sh时它不允许使用此功能,因此您不会无意中依赖于非便携式功能。)

>>> subprocess.Popen(["/bin/bash", "-c", "cat <(head tmp)"])
<subprocess.Popen object at 0x1004cca50>
© www.soinside.com 2019 - 2024. All rights reserved.