1

I am trying to redirect the out of gnu make. Want to redirect ALL to both STDOUT and all.log and error only to 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.

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 ?

  • Which shell are you using? It looks like you're trying to do some none-portable things that work in your shell but not with `sh`. – Biffen Jun 13 '16 at 08:40
  • sh points to bash in centos 6.4. I tried the same command on bash and it works fine and in python the same command with os.system fails with sh errors. – Manjunath C M Jun 13 '16 at 08:45
  • 1
    When you run Bash via a symlink called `sh`, Bash will run in `sh` compatibility mode, and won't let you use any Bashisms. You can try running `/bin/sh` interactively and see the difference. – Biffen Jun 13 '16 at 08:46

2 Answers2

2

os.system start /bin/sh and you have bashism in your command:

>(....)

You will need to start 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 \'\\\'\'.\'\\\'\''
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
0

os.system() calls *nix system() call.

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);

More info : doc

You have to do something like os.system("/bin/bash <command>")

gaganso
  • 2,914
  • 2
  • 26
  • 43