i want to execute bash function in a script and i want to print the return function.
this is my current code
function myfunct() {
return 1
}
and my python code:
process = subprocess.Popen(['bash', '-c', '. myscript.sh; myfunct'])
output, error = process.communicate()
print(output)
i have "None" output, i want 1 output
Konrad Rudolph
550k142 gold badges968 silver badges1.3k bronze badges
1 Answer 1
The return value of a script isn’t streamed to a standard stream. It sets an exit code, which you can query via process.returncode.
Since you aren’t actually reading the script’s standard streams, don’t use Popen/communicate — use run instead:
process = subprocess.run(['bash', '-c', '. myscript.sh; myfunct'])
result = process.returncode
answered Sep 14, 2020 at 14:54
Konrad Rudolph
550k142 gold badges968 silver badges1.3k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py