| Not Working In Subprocess.call

折月煮酒 提交于 2020-01-06 07:01:11

问题


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.

回答1:


You can use subprocess.check_output method and Popen class though I wasn't able to chain both pipe operations. 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.



来源:https://stackoverflow.com/questions/50225840/not-working-in-subprocess-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!