我正在尝试重定向出gnu make。我希望将所有内容都重定向到STDOUT和all.log,并仅将错误重定向到error.log。
下面是我的程序
#!/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当我执行它时,
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 )'但是在linux bash shell上执行相同的命令没有错误。
make all > >(tee -a all.log ) 2>&1 2> >(tee -a error.log )为什么在使用os.system的python脚本中运行时会失败,而在终端/bash shell中不会呢?
发布于 2016-06-13 16:53:20
os.system启动/bin/sh,您的命令中包含bashism:
>(....)您需要启动bash:
os.system("bash -c '{}'".format(command))还请记住,如果在命令中使用单引号,则需要对其进行转义以打印:'\'',例如:
command="ls '\\''.'\\''"
# And it's even worse in single quotes:
command='ls \'\\\'\'.\'\\\'\''发布于 2016-06-13 16:44:32
os.system()调用*nix system()调用。
系统()库函数使用fork(2)创建子进程,该子进程使用execl(3)执行命令中指定的shell命令,如下所示: execl("/bin/sh","sh","-c",command,(char *) 0);
更多信息:doc
你必须做一些像os.system("/bin/bash <command>")这样的事情
https://stackoverflow.com/questions/37785106
复制相似问题