简体   繁体   English

从 python 中运行 bash 脚本

[英]Running bash script from within python

I have a problem with the following code:我对以下代码有疑问:

callBash.py:调用Bash.py:

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

sleep.sh:睡眠.sh:

sleep 10

I want the "end" to be printed after 10s.我希望在 10 秒后打印“结束”。 (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) (我知道这是一个愚蠢的例子,我可以简单地在 python 中睡觉,但是这个简单的 sleep.sh 文件只是作为一个测试)

Making sleep.sh executable and adding shell=True to the parameter list (as suggested in previous answers) works ok.使 sleep.sh 可执行并将shell=True添加到参数列表(如以前的答案中所建议)可以正常工作。 Depending on the search path, you may also need to add ./ or some other appropriate path.根据搜索路径,您可能还需要添加./或其他一些适当的路径。 (Ie, change "sleep.sh" to "./sleep.sh" .) (即,将"sleep.sh"更改为"./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;如果 bash 脚本的第一行是 shell 的路径,则不需要shell=True参数(在 Linux 等 Posix 系统下); for example, #!/bin/bash .例如, #!/bin/bash

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:如果sleep.sh具有 shebang #!/bin/sh并且它具有适当的文件权限 - 运行chmod u+rx sleep.sh以确保它在$PATH中,那么您的代码应该按原样工作:

import subprocess

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

If the script is not in the PATH then specify the full path to it eg, if it is in the current working directory:如果脚本不在 PATH 中,则指定它的完整路径,例如,如果它在当前工作目录中:

from subprocess import call

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

If the script has no shebang then you need to specify shell=True :如果脚本没有 shebang,那么您需要指定shell=True

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

If the script has no executable permissions and you can't change it eg, 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:如果脚本没有可执行权限并且您无法更改它,例如,通过运行os.chmod('sleep.sh', 0o755)那么您可以将脚本作为文本文件读取并将字符串传递给subprocess模块:

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

Actually, you just have to add the shell=True argument:实际上,您只需添加shell=True参数:

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.警告 如果与不受信任的输入结合使用,使用 shell=True 调用系统 shell 可能会带来安全隐患。 See the warning under Frequently Used Arguments for details.有关详细信息,请参阅常用参数下的警告。

source 资源

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).请记住在传递之前使用 str(arg) 将 args 转换为字符串。

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)

Make sure that sleep.sh has execution permissions, and run it with shell=True :确保sleep.sh具有执行权限,并使用shell=True运行它:

#!/usr/bin/python

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

If chmod is not working then you can also try:如果chmod不起作用,那么您也可以尝试:

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

Adding an answer because I was directed here after asking how to run a bash script from python.添加一个答案是因为我在询问如何从 python 运行 bash 脚本后被引导到这里。 You receive an error OSError: [Errno 2] file not found if your script takes in parameters.如果您的脚本接受参数,您会收到错误OSError: [Errno 2] file not found 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])例如,您的脚本接受了睡眠时间参数: subprocess.call("sleep.sh 10")将不起作用,您必须将其作为数组传递: subprocess.call(["sleep.sh", 10])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM