bash Python:子进程“调用”函数找不到路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38231176/
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
Python: Subprocess "call" function can't find path
提问by Ryan Williams
I have a python program that needs to run a bash command on a server. The program works when I run a bash command in my local directory like this:
我有一个需要在服务器上运行 bash 命令的 python 程序。当我在本地目录中运行 bash 命令时,该程序可以运行,如下所示:
import subprocess
from subprocess import call
call(['bash', 'Script_Test.sh'])
import subprocess
from subprocess import call
call(['bash', 'Script_Test.sh'])
However, after SSH'ing to a server and running a similar line of code below with a path on the server to a bash script, I get the error "No such file or directory"
但是,在通过 SSH 连接到服务器并运行下面类似的代码行以及服务器上指向 bash 脚本的路径后,我收到错误消息“没有这样的文件或目录”
call['bash', path]
This doesn't make sense for a number of reasons. I triple checked that the path is correct. I went on Putty, connected to the server on there, and ran a bash command with the same path and it worked, so it can't be the path. I also ran a number of tests to make sure I was SSH'd into the correct server, and I was. I thought there was a security issue on the server with me running bash, so I tried cat instead. Nope, still unable to locate the path.
由于多种原因,这没有意义。我三次检查了路径是否正确。我继续使用 Putty,连接到那里的服务器,并使用相同的路径运行 bash 命令并且它工作正常,所以它不可能是路径。我还进行了一些测试,以确保我通过 SSH 连接到了正确的服务器,结果确实如此。我认为我运行 bash 时服务器上存在安全问题,所以我尝试了 cat 。不行,还是找不到路径。
I'm not super familiar with python subprocesses, so any pointers to anything I'm missing here with "call" would be very helpful.
我对 python 子进程不是很熟悉,所以任何指向我在这里缺少的“调用”的任何指针都会非常有帮助。
回答by Vladislav Martin
Making Sure Your Script is Ready for Execution
确保您的脚本已准备好执行
- Give your script a shebang line
- 给你的脚本一个shebang行
First things first, it is important that you include a shebang linein your script on Unix-like systems. I recommend, for your script's portability, that you use #!/usr/bin/env bash
.
首先,在类 Unix 系统上的脚本中包含一个 shebang 行很重要。为了脚本的可移植性,我建议您使用#!/usr/bin/env bash
.
A Note on File Extensions:
关于文件扩展名的说明:
I would recommend that you remove the .sh
extension from your script. If you are using the Bourne Again Shell (bash
) to execute your script, then using the .sh
extension is misleading. Simply put, the Bourne Shell (sh
) is different than the Bourne Again Shell (bash
)- so don't use a file extension that suggests you are using a different shell than you actually are!
我建议您从脚本中删除.sh
扩展名。如果您正在使用 Bourne Again Shell ( bash
) 来执行您的脚本,那么使用.sh
扩展名会产生误导。简而言之,Bourne Shell ( sh
) 与 Bourne Again Shell ( bash
) 不同- 所以不要使用表明您使用的 shell 与实际情况不同的文件扩展名!
It's not the end of the world if you don't do change your file extension- your script will still be executed as a bash
script if you have the proper bash
shebang line. Still, it is good practice to either use no file extension(Script_Test
-- strongly preferred) or the .bash
file extension(Script_Test.bash
).
如果您不更改文件扩展名,这并不是世界末日——bash
如果您有正确的bash
shebang 行,您的脚本仍将作为脚本执行。尽管如此,最好还是不使用文件扩展名(Script_Test
-- 强烈推荐)或使用.bash
文件扩展名( Script_Test.bash
)。
- Give your script the proper file permissions
- 给你的脚本适当的文件权限
For your purposes, maybe it is only important to give the current user permissions to read and execute the script. In that case, use chmod u+x Script_Test.sh
. What is important here is that the correct user (u+
) / group (g+
) has permissions to execute the script.
出于您的目的,也许只重要的是授予当前用户读取和执行脚本的权限。在这种情况下,请使用chmod u+x Script_Test.sh
. 这里重要的是正确的用户 ( u+
) / 组 ( g+
) 具有执行脚本的权限。
- Make sure that your script's path is in the
$PATH
environment variable
- 确保您的脚本路径在
$PATH
环境变量中
Executing your bash
script in a Python script
bash
在 Python 脚本中执行您的脚本
Once you've followed these steps, your Python script should work as you've called it in your question:
按照这些步骤操作后,您的 Python 脚本应该可以像您在问题中所说的那样工作:
import subprocess
from subprocess import call
your_call = call("Test_Script.sh")
If you would rather not move your script into the $PATH
environment variable, just make sure that you refer to the script's full path (which is the current directory, ./
, in your case):
如果您不想将脚本移动到$PATH
环境变量中,只需确保您引用脚本的完整路径(./
在您的情况下为当前目录):
import subprocess
from subprocess import call
your_call = call("./Test_Script.sh")
Lastly, if your script does not have a shebang line, you will need to specify an additional parameter in your call
function:
最后,如果您的脚本没有 shebang 行,您将需要在您的call
函数中指定一个附加参数:
import subprocess
from subprocess import call
your_call = call("./Test_Script.sh", shell=True)
However, I would not recommend this last approach. See the Python 2.7.12 documentation for the subprocess
package...
但是,我不推荐这最后一种方法。见一个为Python 2.7.12文件subprocess
包...
Warning:Using
shell=True
can be a security hazard. See the warning under Frequently Used Argumentsfor details.
警告:使用
shell=True
可能存在安全隐患。有关详细信息,请参阅常用参数下的警告。
Please check out @zenpoy's explanation to a similar StackOverflow questionif you are still having problems.
如果您仍然遇到问题,请查看@zenpoy 对类似 StackOverflow 问题的解释。
Happy coding!
快乐编码!