bash - Executing A complex Shell command read as String from a File in Python -
i have configuration file user can specify set of shell commands. commands 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 able read commands in python scripts. question how able run these read command strings in python , output.
any solution subprocess
, plumbum
, sh
acceptable.
use subprocess.check_output()
output = subprocess.check_output(output)
something aware of unlike other subprocess commands, subprocess.calledprocesserror raised if non-zero error code returned.
you shouldn't need this, in case comes in handy out there, did run experience once reason above solution did not work, , so, instead, did following.
stdout_fh = io.stringio() stderr_fh = io.stringio() redirect_stderr(stderr_fh): redirect_stdout(stdout_fh): subprocess.run(command, shell=true) stderr = stderr_fh.getvalue() stdout = stderr_fh.getvalue()
Comments
Post a Comment