Skip to content
Advertisement

How to access python return value from bash script

I’m trying to understand how to access from a bash script the return value of a python script.

Clarifying through an example:

foo.py

JavaScript

start.sh

JavaScript

If I run the bash script, this prints the message “exec main..”.

How can I store in script_output the return value (execution ok)? If I direct execution ok to stdout, the script_output will capture all the stdout (so the 2 print statement).

Is there any way to implement this?

Thanks! Alessio

Advertisement

Answer

Add a proper exit code from your script using the sys.exit() module. Usually commands return 0 on successful completion of a script.

JavaScript

and capture it in shell script with a simple conditional. Though the exit code is 0 by default and need not be passed explicitly, using sys.exit() gives control to return non-zero codes on error cases wherever applicable to understand some inconsistencies with the script.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement