tags:

views:

436

answers:

2

I have command like this.

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

And I want its output within python script, i tried this code

>>> 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'
>>>

It seem my code is wrong only wmctrl -lp was execute, and | awk '{print $1}' is omitted My expect output would like 0x03800081

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

Does one please help.

+5  A: 

With shell=True, you should use a single command line instead of an array, otherwise your additional arguments are interpreted as shell arguments. From the subprocess documentation:

On Unix, with shell=True: If args is a string, it specifies the command string to execute through the shell. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional shell arguments.

So your call should be:

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

I think you may also have an unbalanced single quote in there.

Greg Hewgill
A: 

Because you are passing a sequence in for the program, it thinks that the pipe is an argument to wmcrtrl, such as if you did

wmctrl -lp "|"

and thus the actual pipe operation is lost.

Making it a single string should indeed give you the correct result:

>>> 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()
''
Mark Rushakoff