【问题标题】:Python Popen can't open bash shell in CentOS/Red HatPython Popen 无法在 CentOS/Red Hat 中打开 bash shell
【发布时间】:2012-11-10 18:37:40
【问题描述】:

我无法在 CentOS 中使用 python 2.7 打开 bash shell,我可以在 python 2.6.6 Debian 中这样做。发生了什么变化?

我尝试了一个简单的 bash 进程替换:

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

在 Debian 中有效,在 CentOS 中无效:

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

区别在于:

  • Debian:Python 2.6.6,/bin/sh 由 dash 提供。
  • CenOS(Red Hat):Python 2.7,/bin/sh 由 bash 提供

所以在 CentOS 中,executable=/bin/bash 根本不被尊重。 我错过了什么吗?

【问题讨论】:

  • 尝试将cat &lt;(%s)' % cmd替换为cmd
  • @werehuman 这是测试 bash 是否正在启动,sh 可以进行重定向,但不能处理替换,我需要一些不同的东西。
  • 好吧,如果你在参数中写 /bin/bash 呢? Popen(['/bin/bash', '-c', 'cat &lt;(%s)' % cmd])
  • @werehuman 确实有效!这就是执行标志应该做的......不知道为什么不是
  • 我没有 CentOS 机器,但我认为 bash 有问题。

标签: python bash centos debian redhat


【解决方案1】:

如果你在参数中写 /bin/bash 会怎样?

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

【讨论】:

  • 感谢 werehuman,这行得通。我希望有人能指出为什么 Python 2.7 - Red hat 需要这样做。
  • 可能是因为输入重定向是shell的工作,在你的情况下是bash?
猜你喜欢
  • 2022-08-18
  • 2014-10-29
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多