0

I am calling py script from the bash script, but my python script returns me the value which I want to use in the bash script. Can someone plz help with this?

3
  • 2
    Does this answer your question? store return value of a Python script in a bash script Commented Jul 12, 2020 at 14:01
  • This is how I did it but it's not working outputValue=python python-script.py Commented Jul 12, 2020 at 14:18
  • 1
    You need to be clearer about how the Python script returns a value. If you mean you want to get its exit status, you need val=$?. If you mean you want to catch the output it printed, you need val=$(python script.py) Commented Jul 13, 2020 at 11:11

1 Answer 1

0

Hello

It is pretty simple. Just put out what you want with print() and you can use this output. From a python function you also can use return and a string or whatever to give the output as input for bash/sh scripts. In linux all things fine when a program/script exit with a returncode of zero ( 0 ). So the rule should be: Return/Exit/Error Messages in textform: print('whatever') and Returncode: sys.exit(number)

#!/usr/bin/python3
import sys
print('No error - But i return 1 and not 0')
sys.exit(1)

Playing with in bash ( test.py ) ...

# test.py &>/dev/null && echo 'zero' || echo 'not zero'
not zero
# echo -ne $(test.py)'\n'
No error - But i return 1 and not 0

So first command ( short if then else ) checks only for returncode ( exitcode ) and the second use the output. Use both and all things went fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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