问题:从python中运行bash脚本

我的以下代码有问题:

callBash.py:

import subprocess
print "start"
subprocess.call("sleep.sh")
print "end"

sleep.sh:

sleep 10

我希望10秒钟后打印“结束”。(我知道这是一个愚蠢的示例,我可以简单地在python中睡眠,但是这个简单的sleep.sh文件只是一个测试)

I have a problem with the following code:

callBash.py:

import subprocess
print "start"
subprocess.call("sleep.sh")
print "end"

sleep.sh:

sleep 10

I want the “end” to be printed after 10s. (I know that this is a dumb example, I could simply sleep within python, but this simple sleep.sh file was just as a test)


回答 0

使sleep.sh可执行并添加shell=True到参数列表中(如先前答案中所建议)可以正常工作。根据搜索路径,您可能还需要添加./或其他合适的路径。(即,更改"sleep.sh""./sleep.sh"。)

shell=True不需要的参数,如果bash脚本的第一行是一个外壳的路径(一个Posix的系统如Linux下); 例如,#!/bin/bash

Making sleep.sh executable and adding shell=True to the parameter list (as suggested in previous answers) works ok. Depending on the search path, you may also need to add ./ or some other appropriate path. (Ie, change "sleep.sh" to "./sleep.sh".)

The shell=True parameter is not needed (under a Posix system like Linux) if the first line of the bash script is a path to a shell; for example, #!/bin/bash.


回答 1

如果sleep.sh具有shebang #!/bin/sh并且它具有适当的文件权限-运行chmod u+rx sleep.sh以确保它已经在其中,$PATH则您的代码应按原样工作:

import subprocess

rc = subprocess.call("sleep.sh")

如果脚本不在PATH中,则指定它的完整路径,例如,如果它在当前工作目录中:

from subprocess import call

rc = call("./sleep.sh")

如果脚本没有shebang,则需要指定shell=True

rc = call("./sleep.sh", shell=True)

如果脚本没有可执行权限,并且您无法更改它(例如,通过运行),os.chmod('sleep.sh', 0o755)则可以将脚本作为文本文件读取,然后将字符串传递给subprocess模块:

with open('sleep.sh', 'rb') as file:
    script = file.read()
rc = call(script, shell=True)

If sleep.sh has the shebang #!/bin/sh and it has appropriate file permissions — run chmod u+rx sleep.sh to make sure and it is in $PATH then your code should work as is:

import subprocess

rc = subprocess.call("sleep.sh")

If the script is not in the PATH then specify the full path to it e.g., if it is in the current working directory:

from subprocess import call

rc = call("./sleep.sh")

If the script has no shebang then you need to specify shell=True:

rc = call("./sleep.sh", shell=True)

If the script has no executable permissions and you can’t change it e.g., by running os.chmod('sleep.sh', 0o755) then you could read the script as a text file and pass the string to subprocess module instead:

with open('sleep.sh', 'rb') as file:
    script = file.read()
rc = call(script, shell=True)

回答 2

实际上,您只需要添加shell=True参数即可:

subprocess.call("sleep.sh", shell=True)

但要注意-

警告如果与不受信任的输入结合使用,使用shell = True调用系统外壳可能会带来安全隐患。有关详细信息,请参见“常用参数”下的警告。

资源

Actually, you just have to add the shell=True argument:

subprocess.call("sleep.sh", shell=True)

But beware –

Warning Invoking the system shell with shell=True can be a security hazard if combined with untrusted input. See the warning under Frequently Used Arguments for details.

source


回答 3

如果有人希望通过参数调用脚本

import subprocess

val = subprocess.check_call("./script.sh '%s'" % arg, shell=True)

请记住在传递之前使用str(arg)将args转换为字符串。

这可以用来传递任意多个参数:

subprocess.check_call("./script.ksh %s %s %s" % (arg1, str(arg2), arg3), shell=True)

If someone looking for calling a script with arguments

import subprocess

val = subprocess.check_call("./script.sh '%s'" % arg, shell=True)

Remember to convert the args to string before passing, using str(arg).

This can be used to pass as many arguments as desired:

subprocess.check_call("./script.ksh %s %s %s" % (arg1, str(arg2), arg3), shell=True)

回答 4

确保sleep.sh具有执行权限,并使用来运行它shell=True

#!/usr/bin/python

import subprocess
print "start"
subprocess.call("./sleep.sh", shell=True)
print "end"

Make sure that sleep.sh has execution permissions, and run it with shell=True:

#!/usr/bin/python

import subprocess
print "start"
subprocess.call("./sleep.sh", shell=True)
print "end"

回答 5

如果chmod不起作用,那么您也可以尝试

import os
os.system('sh script.sh')
#you can also use bash instead of sh

由我测试谢谢

If chmod not working then you also try

import os
os.system('sh script.sh')
#you can also use bash instead of sh

test by me thanks


回答 6

添加一个答案是因为询问了如何从python运行bash脚本后我被定向到了这里。OSError: [Errno 2] file not found如果您的脚本接受参数,则会收到错误消息。假设您的脚本输入了一个sleep time参数:subprocess.call("sleep.sh 10")将无法工作,您必须将其作为数组传递:subprocess.call(["sleep.sh", 10])

Adding an answer because I was directed here after asking how to run a bash script from python. You receive an error OSError: [Errno 2] file not found if your script takes in parameters. Lets say for instance your script took in a sleep time parameter: subprocess.call("sleep.sh 10") will not work, you must pass it as an array: subprocess.call(["sleep.sh", 10])


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。