python execute multiple command subprocess -
i have command like
command = "su - user; cd $config; grep domain domains.xml"
and need execute commands 1 after other , capture output of grep.
def subprocess_cmd(command): fnlogs("comand = " + command) process = subprocess.popen(command,stdout=subprocess.pipe, shell=true) proc_stdout = process.communicate()[0].strip() fnlogs("proc_stdout = " +proc_stdout + "\n") subprocess_cmd('su - user; cd $config; grep domain domains.xml')
output says grep: domains.xml: no such file or directory, although file exists not able find it.
it seems not passing value config
.
if know intended value within script, can do
d = dict(os.environ) d["config"] = "/some/directory" process = subprocess.popen(command,stdout=subprocess.pipe, shell=true, env=d)
you might have call sudo
-e
preserve environment pass first outermost shell
(see https://stackoverflow.com/a/8633575/693140).
on more general note, subcommand not seem not achieve within python without relying on external commands. recommend reconsider approach altogether, , open file , extract respective text string (eg using regular expressions).
Comments
Post a Comment