Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

def main():
    print ("exec main..")
    return "execution ok"

if __name__ == '__main__':
    main()

start.sh

script_output=$(python foo.py 2>&1)
echo $script_output

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

like image 429
AlessioG Avatar asked Jan 16 '18 11:01

AlessioG


People also ask

Can we return value from bash script?

Return ValuesWhen a bash function completes, its return value is the status of the last statement executed in the function, 0 for success and non-zero decimal number in the 1 - 255 range for failure. The return status can be specified by using the return keyword, and it is assigned to the variable $? .

How do you return a value from a Python script?

The Python return statement is a special statement that you can use inside a function or method to send the function's result back to the caller. A return statement consists of the return keyword followed by an optional return value. The return value of a Python function can be any Python object.

What is $_ in bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.

Can we return value from function in shell script?

A Bash function can't return a string directly like you want it to. You can do three things: Echo a string. Return an exit status, which is a number, not a string.


3 Answers

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

import sys

def main():
    print ("exec main..")
    sys.exit(0)

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.

if python foo.py 2>&1 >/dev/null; then
    echo 'script ran fine'
fi
like image 145
Inian Avatar answered Sep 22 '22 07:09

Inian


You can get the previous command's output status through $?. If the python script ran successfully without any stderr, it should return 0 as exit code else it would return 1 or any number other than 0.

#!/bin/bash
python foo.py 2>&1 /dev/null
script_output=$?
echo $script_output
like image 43
Avinash Raj Avatar answered Sep 25 '22 07:09

Avinash Raj


Bash contains only return code in $?, so you can't use it to print the text from python's return. My solution is write in to the stderr in python script, next print only stderr in bash:

import sys
    
    
def main():
    print ("exec main..")
    sys.stderr.write('execution ok\n')
    return "execution ok"
    
if __name__ == '__main__':
    main()

Bash:

#!/bin/bash
    
script_output=$(python foo.py 1>/dev/null)
echo $script_output

Output:

execution ok
like image 33
Viktor Khilin Avatar answered Sep 22 '22 07:09

Viktor Khilin