从 python 中运行 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13745648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running bash script from within python
提问by user1638145
I have a problem with the following code:
我有以下代码的问题:
callBash.py:
callBash.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. (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)
我希望在 10 秒后打印“结束”。(我知道这是一个愚蠢的例子,我可以简单地在 python 中睡觉,但这个简单的 sleep.sh 文件只是作为一个测试)
采纳答案by James Waldby - jwpat7
Making sleep.sh executable and adding shell=Trueto 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".)
使 sleep.sh 可执行并添加shell=True到参数列表(如先前答案中所建议)可以正常工作。根据搜索路径,您可能还需要添加./或其他一些适当的路径。(即,更改"sleep.sh"为"./sleep.sh"。)
The shell=Trueparameter 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.
shell=True如果 bash 脚本的第一行是 shell 的路径,则不需要该参数(在像 Linux 这样的 Posix 系统下);例如,#!/bin/bash。
回答by Adam Matan
Make sure that sleep.shhas 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"
回答by zenpoy
Actually, you just have to add the shell=Trueargument:
实际上,您只需要添加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. See the warning under Frequently Used Arguments for details.
警告如果与不受信任的输入相结合,使用 shell=True 调用系统 shell 可能会带来安全隐患。有关详细信息,请参阅常用参数下的警告。
回答by jfs
If sleep.shhas the shebang #!/bin/shand it has appropriate file permissions -- run chmod u+rx sleep.shto make sure and it is in $PATHthen 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 e.g., 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 e.g., by running os.chmod('sleep.sh', 0o755)then you could read the script as a text file and pass the string to subprocessmodule instead:
如果脚本没有可执行权限并且您无法更改它,例如,通过运行,os.chmod('sleep.sh', 0o755)那么您可以将脚本作为文本文件读取并将字符串传递给subprocess模块:
with open('sleep.sh', 'rb') as file:
script = file.read()
rc = call(script, shell=True)
回答by Matthew Lang
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 foundif 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])
添加答案是因为我在询问如何从 python 运行 bash 脚本后被定向到这里。OSError: [Errno 2] file not found如果您的脚本接受参数,您会收到错误消息。假设您的脚本接受了一个睡眠时间参数:subprocess.call("sleep.sh 10")不起作用,您必须将其作为数组传递:subprocess.call(["sleep.sh", 10])
回答by Ponmudi VN
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 required
这可用于根据需要传递尽可能多的参数
subprocess.check_call("./script.ksh %s %s %s" % (agr1, str(arg2), arg3), shell=True)
回答by Harry1992
If chmod not working then you also try
如果 chmod 不起作用,那么您也可以尝试
import os
os.system('sh script.sh')
#you can also use bash instead of sh
test by me thanks
由我测试谢谢

