Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这是在Python中运行shell脚本的正确方法吗?_Python_Linux_Bash_Shell_Unix - Fatal编程技术网

这是在Python中运行shell脚本的正确方法吗?

这是在Python中运行shell脚本的正确方法吗?,python,linux,bash,shell,unix,Python,Linux,Bash,Shell,Unix,当我运行这两条线路时,我会完全这样做吗 import subprocess retcode = subprocess.call(["/home/myuser/go.sh", "abc.txt", "xyz.txt"]) 为什么会出现这个错误?但是,当我正常运行go.sh时,我不会得到那个错误 /home/myuser/go.sh abc.txt xyz.txt 是的,如果您所做的只是调用shell脚本,等待它完成,并收集它的退出状态,同时让它的stdin、stdout和stderr从您的Py

当我运行这两条线路时,我会完全这样做吗

import subprocess
retcode = subprocess.call(["/home/myuser/go.sh", "abc.txt", "xyz.txt"])
为什么会出现这个错误?但是,当我正常运行go.sh时,我不会得到那个错误

/home/myuser/go.sh abc.txt xyz.txt

是的,如果您所做的只是调用shell脚本,等待它完成,并收集它的退出状态,同时让它的stdin、stdout和stderr从您的Python进程继承,那么这很好。如果您需要对这些因素中的任何一个进行更多的控制,那么您只需使用更通用的方法,但在其他情况下,您所拥有的就可以了。

是的,这是执行某些操作的首选方法

由于您通过数组传递所有参数(该数组将在内部用于exec()样式的调用),而不是作为shell计算的参数字符串,因此也非常安全,因为不可能插入shell命令

OSError:[Errno 8]Exec格式错误

这是尝试运行
/home/myuser/go.sh
时操作系统报告的错误

在我看来,
go.sh
的shebang(
#!
)行无效

下面是一个从shell运行的示例脚本,但不是从
Popen

File "/usr/lib/python2.6/subprocess.py", line 480, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.6/subprocess.py", line 633, in __init__
    errread, errwrite)
  File "/usr/lib/python2.6/subprocess.py", line 1139, in _execute_child
    raise child_exception
OSError: [Errno 8] Exec format error

从第一行删除
\
可以解决问题。

将代码更改为以下内容:

#\!/bin/sh
echo "You've just called $0 $@."
注意“shell=True”

发件人:

在Unix上,shell=True时:如果args是 字符串,它指定命令 要通过shell执行的字符串。 这意味着字符串必须是 格式完全符合 在shell提示下键入


我刚刚在Mac OS上遇到这个错误,当时我正试图使用
subprocess.call调用一行脚本。从命令行调用脚本时,脚本运行良好。添加shebang行
#后/usr/bin/env sh
,通过
子进程调用它也可以正常运行


看起来,虽然shell有一个标记为可执行的文本文件的默认执行器,
subprocess.Popen
没有。

我最近遇到了这样一个脚本问题:

retcode = subprocess.call(["/home/myuser/go.sh", "abc.txt", "xyz.txt"], shell=True,)
通过以下方式执行时:

OSError: [Errno 8] Exec format error
(当然,解决办法是删除空行)


调用刚才调用的Popen,使用wait()方法等待popenargs完成

您能告诉我为什么会出现此错误:OSError:[Errno 8]Exec format error。当我正常运行它时,它运行正常。对于运行python脚本的用户,脚本是否可执行?您的shell脚本是否具有正确的hashbang?您是否解决过此问题?您能否告诉我为什么会出现此错误:OSError:[Errno 8]Exec format error。当我正常运行它时,它运行正常。类似的错误也发生在我身上,第一行是一些bash注释,然后是shebang。因此python没有提交脚本,但当我将shebang放在第一行时,它起了作用。您能解释一下原因吗?@user1004985:shebang行必须是文件中的第一行。当然。非常感谢你,你真的救了我的命。不,这是不正确的;
shell=True
的效果是,您将运行
sh-c'/home/myuser/go.sh'“abc.txt”“xyz.txt”
,最后将第一个参数丢失到
$0
中,将第二个参数丢失到
$1
中,其中使用此特定脚本的
sh
将忽略它。
OSError: [Errno 8] Exec format error
subprocess.Popen(['/tmp/test.sh']).communicate()
In :call??
Signature: call(*popenargs, **kwargs)
Source:   
def call(*popenargs, **kwargs):
    """Run command with arguments.  Wait for command to complete, then
    return the returncode attribute.

    The arguments are the same as for the Popen constructor.  Example:

    retcode = call(["ls", "-l"])
    """
    return Popen(*popenargs, **kwargs).wait()
File:      /usr/lib64/python2.7/subprocess.py
Type:      function