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?

  • 2
    Does this answer your question? [store return value of a Python script in a bash script](https://stackoverflow.com/questions/11900828/store-return-value-of-a-python-script-in-a-bash-script) – limido Jul 12 '20 at 14:01
  • This is how I did it but it's not working ```outputValue=python python-script.py``` –  Jul 12 '20 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)` – Mark Setchell Jul 13 '20 at 11:11

1 Answers1

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.

koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15