2

I Have a shell script which in turn runs a python script, I have to exit from the main shell script when an exception is caught in python. Can anyone suggest a way on how to achieve it.

Deepak
  • 377
  • 4
  • 14

1 Answers1

2

In Python you can set the return value using sys.exit(). Typically when execution completed successfully you return 0, and if not then some non-zero number.

So something like this in your Python will work:

import sys

try:
    ....
except:
    sys.exit(1)

And then, as others have said, you need to make sure your bash script catches the error by either checking the return value explicitly (using e.g. $?) or using set -e.

Duncan WP
  • 830
  • 5
  • 19