在python中运行bash脚本

时间:2021-09-02 00:08:38

I have a problem with the following code:

我对下面的代码有一个问题:

callBash.py:

callBash.py:

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

sleep.sh:

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)

我想把“结尾”印在10点以后。(我知道这是一个愚蠢的例子,我可以简单地睡在python里,但是这个简单的睡眠。sh文件只是一个测试)

7 个解决方案

#1


51  

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".)

使睡眠。sh可执行文件和向参数列表中添加shell=True(如前面的答案所示)可以正常工作。根据搜索路径的不同,您可能还需要添加./或其他适当的路径。(例如,改变“睡眠。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; for example, #!/bin/bash.

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

#2


22  

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. See the warning under Frequently Used Arguments for details.

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

source

#3


20  

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:

如果睡眠。嘘,嘘!它有适当的文件权限——运行chmod u+rx睡眠。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:

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

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 subprocess module instead:

如果脚本没有可执行权限,并且您不能通过运行os.chmod('sleep)来更改它。然后你可以把脚本当作文本文件来读,然后把字符串传递给子进程模块:

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

#4


5  

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

确保睡眠。sh具有执行权限,运行时shell=True:

#!/usr/bin/python

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

#5


2  

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).

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

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)

#6


1  

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])

添加一个答案,因为我是在询问如何从python中运行bash脚本之后被指导的。如果您的脚本接受参数,则会收到一个错误的OSError: [Errno 2]文件。比方说,您的脚本接受了一个睡眠时间参数:subprocess.call(“sleep”)。sh10 ")不能工作,您必须将它作为一个数组传递:subprocess.call(["sleep ")。sh”,10])

#7


0  

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

测试由我谢谢

#1


51  

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".)

使睡眠。sh可执行文件和向参数列表中添加shell=True(如前面的答案所示)可以正常工作。根据搜索路径的不同,您可能还需要添加./或其他适当的路径。(例如,改变“睡眠。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; for example, #!/bin/bash.

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

#2


22  

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. See the warning under Frequently Used Arguments for details.

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

source

#3


20  

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:

如果睡眠。嘘,嘘!它有适当的文件权限——运行chmod u+rx睡眠。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:

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

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 subprocess module instead:

如果脚本没有可执行权限,并且您不能通过运行os.chmod('sleep)来更改它。然后你可以把脚本当作文本文件来读,然后把字符串传递给子进程模块:

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

#4


5  

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

确保睡眠。sh具有执行权限,运行时shell=True:

#!/usr/bin/python

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

#5


2  

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).

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

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)

#6


1  

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])

添加一个答案,因为我是在询问如何从python中运行bash脚本之后被指导的。如果您的脚本接受参数,则会收到一个错误的OSError: [Errno 2]文件。比方说,您的脚本接受了一个睡眠时间参数:subprocess.call(“sleep”)。sh10 ")不能工作,您必须将它作为一个数组传递:subprocess.call(["sleep ")。sh”,10])

#7


0  

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

测试由我谢谢