繁体   English   中英

如何从外部命令与Pipe结合获得输出

[英]How to get output from external command combine with Pipe

我有这样的命令。

wmctrl -lp | awk '/gedit/ { print $1 }'

我想要它在python脚本中的输出,我尝试了这段代码

>>> import subprocess
>>> proc =  subprocess.Popen(["wmctrl -lp", "|","awk '/gedit/ {print $1}"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> proc.stdout.readline()
'0x0160001b -1 6504   beer-laptop x-nautilus-desktop\n'
>>> proc.stdout.readline()
'0x0352f117  0 6963   beer-laptop How to get output from external command combine with Pipe - Stack Overflow - Chromium\n'
>>> proc.stdout.readline()
'0x01400003 -1 6503   beer-laptop Bottom Expanded Edge Panel\n'
>>> 

似乎我的代码是错误的,仅执行了wmctrl -lp ,并且| awk '{print $1}' | awk '{print $1}'被省略了我的期望输出是0x03800081

$ wmctrl -lp | awk '/gedit/ {print $1}'
0x03800081

请帮助。

如果使用shell=True ,则应使用单个命令行而不是数组,否则,其他参数将被解释为shell参数。 subprocess文档中

在Unix上,如果shell = True:如果args是字符串,则它指定要通过shell执行的命令字符串。 如果args是序列,则第一项指定命令字符串,任何其他项都将被视为其他shell参数。

因此,您的电话应该是:

subprocess.Popen("wmctrl -lp | sed /gedit/ '{print $1}'", shell=True, ...

我认为您可能在其中也有不平衡的单引号。

因为您正在传递程序的序列,所以它认为管道是wmcrtrl参数 ,例如

wmctrl -lp "|"

因此,实际的管道操作会丢失。

使其成为单个字符串确实应该为您提供正确的结果:

>>> import subprocess as s
>>> proc = s.Popen("echo hello | grep e", shell=True, stdout=s.PIPE, stderr=s.PIPE)
>>> proc.stdout.readline()
'hello\n'
>>> proc.stdout.readline()
''

经过研究,我得到了下面的代码,这些代码对我来说非常有效。 它基本上可以实时打印stdout和stderr。 希望它可以帮助需要它的人。

stdout_result = 1
stderr_result = 1


def stdout_thread(pipe):
    global stdout_result
    while True:
        out = pipe.stdout.read(1)
        stdout_result = pipe.poll()
        if out == '' and stdout_result is not None:
            break

        if out != '':
            sys.stdout.write(out)
            sys.stdout.flush()


def stderr_thread(pipe):
    global stderr_result
    while True:
        err = pipe.stderr.read(1)
        stderr_result = pipe.poll()
        if err == '' and stderr_result is not None:
            break

        if err != '':
            sys.stdout.write(err)
            sys.stdout.flush()


def exec_command(command, cwd=None):
    if cwd is not None:
        print '[' + ' '.join(command) + '] in ' + cwd
    else:
        print '[' + ' '.join(command) + ']'

    p = subprocess.Popen(
        command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd
    )

    out_thread = threading.Thread(name='stdout_thread', target=stdout_thread, args=(p,))
    err_thread = threading.Thread(name='stderr_thread', target=stderr_thread, args=(p,))

    err_thread.start()
    out_thread.start()

    out_thread.join()
    err_thread.join()

    return stdout_result + stderr_result

在需要时,我认为很容易将输出或错误收集到字符串中并返回。

暂无
暂无

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

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