问题描述
我很难解析 subprocess.Popen 的参数.我正在尝试在我的 Unix 服务器上执行脚本.在 shell 提示符下运行时的脚本语法如下:/usr/local/bin/script 主机名 = <主机名>-p 长列表
.无论我如何尝试,脚本都没有在 subprocess.Popen 中运行
I am having hard time parsing the arguments to subprocess.Popen. I am trying to execute a script on my Unix server. The script syntax when running on shell prompt is as follows:
/usr/local/bin/script hostname = <hostname> -p LONGLIST
. No matter how I try, the script is not running inside subprocess.Popen
="前后的空格为必填项.
The space before and after "=" is mandatory.
import subprocess
Out = subprocess.Popen(['/usr/local/bin/script', 'hostname = ', 'actual server name', '-p', 'LONGLIST'],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
上述方法不起作用.
当我使用 shell=False 时,我得到 OSError: [Errno 8] Exec format error
And when I use shell=False, I get OSError: [Errno 8] Exec format error
推荐答案
OSError: [Errno 8] Exec format error
如果shell脚本顶部没有shebang行并且你正在尝试直接执行脚本.这是一个重现该问题的示例:
OSError: [Errno 8] Exec format error
can happen if there is no shebang line at the top of the shell script and you are trying to execute the script directly. Here's an example that reproduces the issue:
>>> with open('a','w') as f: f.write('exit 0') # create the script
...
>>> import os
>>> os.chmod('a', 0b111101101) # rwxr-xr-x make it executable
>>> os.execl('./a', './a') # execute it
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/os.py", line 312, in execl
execv(file, args)
OSError: [Errno 8] Exec format error
要修复它,只需添加 shebang,例如,如果它是一个 shell 脚本;在脚本顶部添加 #!/bin/sh
:
To fix it, just add the shebang e.g., if it is a shell script; prepend #!/bin/sh
at the top of your script:
>>> with open('a','w') as f: f.write('#!/bin/sh
exit 0')
...
>>> os.execl('./a', './a')
它执行 exit 0
没有任何错误.
It executes exit 0
without any errors.
在 POSIX 系统上,shell 会解析命令行,也就是说,您的脚本不会在 =
周围看到空格,例如,如果 script
是:
On POSIX systems, shell parses the command line i.e., your script won't see spaces around =
e.g., if script
is:
#!/usr/bin/env python
import sys
print(sys.argv)
然后在 shell 中运行它:
then running it in the shell:
$ /usr/local/bin/script hostname = '<hostname>' -p LONGLIST
产生:
['/usr/local/bin/script', 'hostname', '=', '<hostname>', '-p', 'LONGLIST']
注意:'='
周围没有空格.我在 <hostname>
周围添加了引号以转义重定向元字符 <>
.
Note: no spaces around '='
. I've added quotes around <hostname>
to escape the redirection metacharacters <>
.
要在 Python 中模拟 shell 命令,请运行:
To emulate the shell command in Python, run:
from subprocess import check_call
cmd = ['/usr/local/bin/script', 'hostname', '=', '<hostname>', '-p', 'LONGLIST']
check_call(cmd)
注意:没有 shell=True
.而且您不需要转义 <>
因为没有运行任何 shell.
Note: no shell=True
. And you don't need to escape <>
because no shell is run.
"Exec format error"
可能表示您的script
格式无效,请运行:
"Exec format error"
might indicate that your script
has invalid format, run:
$ file /usr/local/bin/script
找出它是什么.将架构与以下输出进行比较:
to find out what it is. Compare the architecture with the output of:
$ uname -m
这篇关于OSError: [Errno 8] 执行格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!