不能在子进程命令中使用 grep [英] Cant use grep in subprocess command

查看:34
本文介绍了不能在子进程命令中使用 grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的子进程命令有问题,我喜欢 grep 出与在线"行匹配的行.

I'm having a problem with my subprocess command, I like to grep out the lines that match with "Online" line.

def run_command(command):
    p = subprocess.Popen(command,shell=False,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    return iter(p.stdout.readline, b'')

command = 'mosquitto_sub -u example -P example -t ITT/# -v | grep "Online" '.split()
for line in run_command(command):
    print(line)

但是我会得到一个错误

Error: Unknown option '|'.



Use 'mosquitto_sub --help' to see usage.

但是当用 linux shell 运行时

But when running with linux shell

user@server64:~/Pythoniscriptid$ mosquitto_sub -u example -P example  -t ITT/# -v | grep "Online"
ITT/C5/link Online
ITT/IoT/tester55/link Online
ITT/ESP32/TEST/link Online

我也尝试过 shell = True,但没有成功,因为我会得到另一个错误,即无法识别主题 ITT/#

I also tried shell = True, but with no success, because I will get another error, that dosen't recognize the topic ITT/#

Error: You must specify a topic to subscribe to.



Use 'mosquitto_sub --help' to see usage.

可能的重复"根本没有帮助我,所以我想我遇到了不同的问题.我试着把代码改成这个,没有得到任何回报

The "possible dublicate" didn't help me at all, So I think I'm having a different problem. I tried to change code to this, put in not getting any return

def run_command(command,command2):
    p1 = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    p2 = subprocess.Popen(command2,stdin=p1.stdout,stdout=subprocess.PIPE)
    return iter(p2.stdout.readline,'')



command = 'mosquitto_sub -u example -P example -t ITT/# -v'.split()
command2 = 'grep Online'.split()

#subprocess.getoutput(command)

for line in run_command(command,command2):
    print(line)

推荐答案

拆分文本时,列表看起来像

When you split the text, the list will look like

['mosquitto_sub', ..., 'ITT/#', '-v', '|', 'grep', '"Online"']

当您将此列表传递给 subprocess.Popen 时,文字 '|' 将成为 mosquitto_sub 的参数之一.

When you pass this list to subprocess.Popen, a literal '|' will be one of the arguments to mosquitto_sub.

如果你使用shell=True,你必须在命令中转义任何特殊字符,比如#,例如使用双引号:

If you use shell=True, you must escape any special characters like # in the command, for instance with double quotes:

import subprocess
command = 'echo -e "ITT/#\ni am Online\nbar Online\nbaz" | grep "Online" '
p = subprocess.Popen(
    command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(p.stdout.readline, b''):
    print(line)

或者,按照您编写的方式连接管道,但确保迭代直到 b'',而不是 u'':

Alternatively, connect the pipes as you wrote, but make sure to iterate until b'', not u'':

import subprocess

def run_command(command, command2):
    p1 = subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    p2 = subprocess.Popen(command2,stdin=p1.stdout,stdout=subprocess.PIPE)
    return iter(p2.stdout.readline, b'')

command = ['echo', '-e', 'ITT/#\ni am Online\nbar Online\nbaz']
command2 = 'grep Online'.split()

for line in run_command(command,command2):
    print(line)

这篇关于不能在子进程命令中使用 grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆