I have a configuration file where user can specify a set of shell commands. Commands are basically a chain of pipe-separated shell commands.
CMD1 = grep "SomeOtherString" | grep "XX" | cut -d":" -f9 | cut -d"," -f1
CMD2 = grep "SomeOtherString" | tail -1| cut -d":" -f9 | cut -d"," -f1 | cut -d"[" -f2 | cut -d"]" -f1
I am able to read the commands in my Python scripts. My question is how will I be able to run these read command strings in Python and get the output.
Any solution with subprocess
, plumbum
, sh
will be acceptable.
Use subprocess.check_output ()
output = subprocess.check_output(output)
Something to be aware of is that unlike the other subprocess commands, a subprocess.CalledProcessError will be raised if a non-zero error code is returned.
You shouldn't need to do this, but in case it comes in handy to somebody out there, I did run into an experience once where for some reason the above solution did not work, and so, instead, I did the following.
stdout_fh = io.StringIO()
stderr_fh = io.StringIO()
with redirect_stderr(stderr_fh):
with redirect_stdout(stdout_fh):
subprocess.run(command, shell=True)
stderr = stderr_fh.getvalue()
stdout = stderr_fh.getvalue()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.