I read every thread I found on StackOverflow on invoking shell commands from Python using subprocess
, but I couldn't find an answer that applies to my situation below:
I would like to do the following from Python:
Run shell command command_1
. Collect the output in variable result_1
Shell pipe result_1
into command_2
and collect the output on result_2
. In other words, run command_1 | command_2
using the result that I obtained when running command_1
in the step before
Do the same piping result_1
into a third command command_3
and collecting the result in result_3
.
So far I have tried:
p = subprocess.Popen(command_1, stdout=subprocess.PIPE, shell=True)
result_1 = p.stdout.read();
p = subprocess.Popen("echo " + result_1 + ' | ' +
command_2, stdout=subprocess.PIPE, shell=True)
result_2 = p.stdout.read();
the reason seems to be that "echo " + result_1
does not simulate the process of obtaining the output of a command for piping.
Is this at all possible using subprocess? If so, how?
To use a pipe with the subprocess module, you have to pass shell=True . In your particular case, however, the simple solution is to call subprocess. check_output(('ps', '-A')) and then str. find on the output.
Python Subprocess Run Function The subprocess. run() function was added in Python 3.5 and it is recommended to use the run() function to execute the shell commands in the python program. The args argument in the subprocess. run() function takes the shell command and returns an object of CompletedProcess in Python.
After reading the docs, I came to know that shell=True means executing the code through the shell. So that means in absence, the process is directly started.
By default, running subprocess. Popen with shell=True uses /bin/sh as the shell. If you want to change the shell to /bin/bash , set the executable keyword argument to /bin/bash .
You can do:
pipe = Popen(command_2, shell=True, stdin=PIPE, stdout=PIPE)
pipe.stdin.write(result_1)
pipe.communicate()
instead of the line with the pipe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With