错误号 13:将 Python 变量传递给 bash 脚本
Errno 13: Passing Python variables to bash script
目前我正在测试一段非常简单的代码。我只是想写一个 python 脚本来设置一个变量,然后将该变量传递给 bash 脚本以供使用。
Python 脚本:
from subprocess import check_call
a = str(3)
check_call(["/home/desktop/bash2pyTest/test.sh", a], shell=False)
Bash 脚本:
#!/bin/bash
echo "This number was sent from the py script: "
我已阅读与此主题相关的其他问答;但是,我没有找到我在概念上理解的解决方案;因此,上面的语法可能不正确。我也尝试了其他一些方法;但是,我不断收到以下错误:
Traceback (most recent call last):
File "/home/cassandra/desktop/bash2pyTest/callVar.py", line 3, in <module>
check_call(["/home/cassandra/desktop/bash2pyTest/test.sh", a], shell=False)
File "/usr/lib64/python2.7/subprocess.py", line 537, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib64/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
Process finished with exit code 1
如有任何帮助,我们将不胜感激。我被难住了。
错误提示权限被拒绝。我测试了你的代码,它在我的机器上运行良好。但是,您需要确保用户 运行 命令对 test.sh
脚本具有足够的权限。最重要的是,确保 test.sh
设置了执行权限。这通常是最容易错过的权限。
尝试
chmod +x /home/desktop/bash2pyTest/test.sh
在 shell 中。您尝试执行的文件不可执行。
或 python 脚本中的另一个选项:
check_call(["sh","/home/desktop/bash2pyTest/test.sh", a], shell=False)
目前我正在测试一段非常简单的代码。我只是想写一个 python 脚本来设置一个变量,然后将该变量传递给 bash 脚本以供使用。
Python 脚本:
from subprocess import check_call
a = str(3)
check_call(["/home/desktop/bash2pyTest/test.sh", a], shell=False)
Bash 脚本:
#!/bin/bash
echo "This number was sent from the py script: "
我已阅读与此主题相关的其他问答;但是,我没有找到我在概念上理解的解决方案;因此,上面的语法可能不正确。我也尝试了其他一些方法;但是,我不断收到以下错误:
Traceback (most recent call last):
File "/home/cassandra/desktop/bash2pyTest/callVar.py", line 3, in <module>
check_call(["/home/cassandra/desktop/bash2pyTest/test.sh", a], shell=False)
File "/usr/lib64/python2.7/subprocess.py", line 537, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib64/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
Process finished with exit code 1
如有任何帮助,我们将不胜感激。我被难住了。
错误提示权限被拒绝。我测试了你的代码,它在我的机器上运行良好。但是,您需要确保用户 运行 命令对 test.sh
脚本具有足够的权限。最重要的是,确保 test.sh
设置了执行权限。这通常是最容易错过的权限。
尝试
chmod +x /home/desktop/bash2pyTest/test.sh
在 shell 中。您尝试执行的文件不可执行。
或 python 脚本中的另一个选项:
check_call(["sh","/home/desktop/bash2pyTest/test.sh", a], shell=False)