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()
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.