[英]Error when using os.system in python but not in shell
I am trying to redirect the out of gnu make. 我试图重定向gnu make。 Want to redirect ALL to both STDOUT and all.log and error only to error.log. 想要将ALL重定向到STDOUT和all.log,只将错误重定向到error.log。
Below is my program 以下是我的计划
#!/usr/bin/env python
import optparse
import os
import sys
import commands
command = 'make all > >(tee -a all.log ) 2>&1 2> >(tee -a error.log )'
SysReturnVal=os.system(command)
print "system return value is ", SysReturnVal
When I execute it getting 当我执行它时
sh: -c: line 0: syntax error near unexpected token `>'
sh: -c: line 0: `make all > >(tee -a all.log ) 2>&1 2> >(tee -a error.log )'
But executing the same command on linux bash shell executes without error. 但是在linux bash shell上执行相同的命令会毫无错误地执行。
make all > >(tee -a all.log ) 2>&1 2> >(tee -a error.log )
Why is this failing when running in python script using os.system, but not in terminal/bash shell ? 为什么在使用os.system在python脚本中运行时失败,但在终端/ bash shell中没有?
os.system()
calls *nix system()
call. os.system()
调用* nix system()
调用。
The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows: execl("/bin/sh", "sh", "-c", command, (char *) 0); system()库函数使用fork(2)创建一个子进程,该进程使用execl(3)执行命令中指定的shell命令,如下所示:execl(“/ bin / sh”,“sh”,“ - c”,命令,(char *)0);
You have to do something like os.system("/bin/bash <command>")
你必须做一些像os.system("/bin/bash <command>")
os.system
start /bin/sh
and you have bashism in your command: os.system
start /bin/sh
你的命令中有bashism:
>(....)
You will need to start bash: 你需要开始bash:
os.system("bash -c '{}'".format(command))
Also remember if you use single quotes in your command they need to be escaped to print: '\\''
, eg: 还要记住,如果在命令中使用单引号,则需要将其转义为打印: '\\''
,例如:
command="ls '\\''.'\\''"
# And it's even worse in single quotes:
command='ls \'\\\'\'.\'\\\'\''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.