拒绝通过SubProcess.call运行Shell脚本的权限

发布于 2025-02-11 23:17:04 字数 451 浏览 2 评论 0原文

我使用python编写了一个shell脚本,它通过命令bash download_r1.sh在我的终端上成功运行。

我想测试子过程是否可以在Python中执行相同的操作,因为我想将管道集成在Python脚本中。这是我代码的子过程部分:

downR1 = subprocess.call('./download_R1.sh')

它未能运行以下错误消息来运行脚本:

Error message:
PermissionError: [Errno 13] Permission denied: './download_R1.sh'

有人建议使用chmod +x获得该脚本的授权。但是bash down.sh正在工作。我不知道哪一部分出错。

有建议吗?

I wrote a shell script using python and it successfully run by command bash download_R1.sh on my terminal.

I wanted to test if subprocess can do the same thing in python because I would like to integrate a pipeline in python script. Here is the subprocess part of my code:

downR1 = subprocess.call('./download_R1.sh')

It failed to run the script with following error message:

Error message:
PermissionError: [Errno 13] Permission denied: './download_R1.sh'

Someone suggested using chmod +x to obtain authenitification of that script. But bash down.sh is working. I don't know which part goes wrong.

Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

变身佩奇 2025-02-18 23:17:04

这是外壳行为的正常方式。

如果没有执行特权(+X

,则 无法直接运行程序或脚本或任何运行文件的内容,即使没有+x

foo.sh

#!/bin/bash

# If a parameter is received in the command $1, assign it to $NAME
# if not use the default "World"
NAME="${1:-World}"

echo "Hello ${NAME}"

foo.py.py

#!/usr/bin/env python
print "Hello World"
$ bash foo.sh
Hello World

$ bash foo.sh Person
Hello Person

$ ./foo.sh
bash: ./foo.sh: Permission denied

$ python foo.py
Hello World


$ ./foo.py
bash: ./foo.sh: Permission denied

$ chmod +x foo.sh foo.py

$ ./foo.sh
Hello World
$ ./foo.py
Hello World

在您的情况下,如果您想运行程序使用subprocess,但不给您必须使用的文件执行特权:

subprocess.run(["bash", "download_R1.sh"])

如果您需要将参数传递给脚本,只需将更多项目添加到列表中:

subprocess.run(["bash", "download_R1.sh", "argument1"])

有关使用子程序的一些问题=“ https://stackoverflow.com/questions/11679936/python-subprocess-arguments”>喜欢这个。

如果您喜欢写入命令,请仅建议使用shell = true不推荐。但是您可以使用类似的东西:

subprocess.run("bash download_R1.sh argument1".split())

That's the normal way the shell behaves.

You can not directly run a program or script if it not has execution privileges (+x)

But, you can instruct other program bash, python or whatever to run a file even if not has +x

foo.sh

#!/bin/bash

# If a parameter is received in the command $1, assign it to $NAME
# if not use the default "World"
NAME="${1:-World}"

echo "Hello ${NAME}"

foo.py

#!/usr/bin/env python
print "Hello World"
$ bash foo.sh
Hello World

$ bash foo.sh Person
Hello Person

$ ./foo.sh
bash: ./foo.sh: Permission denied

$ python foo.py
Hello World


$ ./foo.py
bash: ./foo.sh: Permission denied

$ chmod +x foo.sh foo.py

$ ./foo.sh
Hello World
$ ./foo.py
Hello World

In your case, if you want to run the program with subprocess but not giving execution privileges to the file you must use:

subprocess.run(["bash", "download_R1.sh"])

If you need to pass arguments to the script just add more items to the list:

subprocess.run(["bash", "download_R1.sh", "argument1"])

There are several questions about using subprocess an arguments like this one.

If you prefer write you command in only one string be advised that use shell=True is not recommended. But you can use something like:

subprocess.run("bash download_R1.sh argument1".split())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文