简体   繁体   English

| 在Subprocess.call中不起作用

[英]| Not Working In Subprocess.call

Whenever I use a command in a subprocess with "|" 每当我在带有“ |”的子过程中使用命令时 in it doesn't work it has an output of Command "|" 在它不起作用的情况下,它具有命令“ |”的输出 is unknown, try "in link help". 未知,请尝试“链接帮助”。 Or when I put this: 或当我把这个:

#!/usr/bin/python
from subprocess import call
from shlex import split

interface = call(split("ip -o link show | awk '{print $2}' | grep wl"))

It is giving the output of: 它给出以下输出:

Error: either "dev" is duplicate, or "awk" is a garbage.

You can use subprocess.check_output method and Popen class though I wasn't able to chain both pipe operations. 尽管我无法链接两个管道操作,但是您可以使用subprocess.check_output方法和Popen类。 Partial solution: 部分解决方案:

from subprocess import check_output, Popen, PIPE
from shlex import split

process = Popen(split('ip -o link show'), stdout=PIPE)
output = check_output(('awk', '{print $2}'), stdin=process.stdout)
return_code = process.wait()
print(output, return_code)

So basically, awk is taking the process standard output, and result is saved in the output variable. 因此,基本上,awk将采用process标准输出,并将结果保存在output变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM