Python shell脚本无法使用source命令执行。

huangapple go评论65阅读模式
英文:

Python shell script cannot be executed with source command

问题

我想使用`subprocess.Popen()`从Python脚本中调用一个bash脚本调用shell脚本作为可执行文件是有效的但是使用`source`命令则不行为什么

***test_python.py***文件

```python
import sys
import os
import subprocess

os.putenv("testvar", "testvalue")

test = subprocess.Popen("./test_shell.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(test)
test = subprocess.Popen(". test_shell.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(test)
test = subprocess.Popen("source test_shell.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(test)
test = subprocess.Popen("/bin/bash test_shell.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(test)
test = subprocess.Popen("/bin/sh test_shell.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(test)

test_shell.sh文件:

#!/bin/bash

echo "$testvar"

python test_python.py的输出:

('testvalue\n', '')
('', '/bin/sh: .: test_shell.sh: 无法打开文件 [文件或目录不存在]\n')
('', '/bin/sh: .: test_shell.sh: 无法打开文件 [文件或目录不存在]\n')
('testvalue\n', '')
('testvalue\n', '')
英文:

I want to call a bash script from a python script using subprocess.Popen(). Calling the shell script as an executable works, but sourceing it does not. Why?

File test_python.py:

import sys
import os
import subprocess

os.putenv("testvar", "testvalue")

test = subprocess.Popen("./test_shell.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(test)
test = subprocess.Popen(". test_shell.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(test)
test = subprocess.Popen("source test_shell.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(test)
test = subprocess.Popen("/bin/bash test_shell.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(test)
test = subprocess.Popen("/bin/sh test_shell.sh", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(test)

File test_shell.sh:

#!/bin/bash

echo "$testvar"

Output of python test_python.py:

('testvalue\n', '')
('', '/bin/sh: .: test_shell.sh: cannot open [No such file or directory]\n')
('', '/bin/sh: .: test_shell.sh: cannot open [No such file or directory]\n')
('testvalue\n', '')
('testvalue\n', '')

答案1

得分: 1

调用shell脚本作为可执行文件是有效的,但作为源码是无效的。

你正在调用./test_shell.sh。然而,你使用test_shell.sh作为源码。

为什么?

因为在PATH中没有这样的脚本。

如果你想要源码./test_shell.sh,那么应该使用source ./test_shell.sh而不是source test_shell.sh

英文:

> Calling the shell script as an executable works, sourceing it does not.

You are calling ./test_shell.sh. Yet, you source test_shell.sh.

> Why?

Because there is no such script in PATH

If you want to source ./test_shell.sh then source ./test_shell.sh not source test_shell.sh.

huangapple
  • 本文由 发表于 2023年7月17日 19:14:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定