-->

Python Popen can't open bash shell in CentOS/R

2019-09-08 20:35发布

问题:

I can't open a bash shell in CentOS with python 2.7, I'm able to do so in python 2.6.6 Debian. What has changed?

I tried a simple bash process substitution:

from subprocess import Popen
cmd="""cat <<'EOF'
this is
test $unchanged
EOF
"""
Popen('cat <(%s)' % cmd, shell=True, executable='/bin/bash')

In Debian this works, in CentOS it doesn't:

/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `cat <(cat <<'EOF''

The differences are:

  • Debian: Python 2.6.6, /bin/sh is provided by dash.
  • CenOS (Red Hat): Python 2.7, /bin/sh is provided by bash

So in CentOS the executable=/bin/bash is not being honored at all. Am I missing something?

回答1:

What if you write /bin/bash in arguments?

Popen(['/bin/bash', '-c', 'cat <(%s)' % cmd])