0

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
asked Sep 14, 2020 at 14:51

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.