[Tutor] Error Handling in python

Chris “Kwpolska” Warrick kwpolska at gmail.com
Thu Jul 24 13:53:14 CEST 2014


On Thu, Jul 24, 2014 at 1:35 PM, jitendra gupta <jitu.icfai at gmail.com> wrote:
> Hi All
>
> My shell script is not throwing any error when I am having  some error in
> Python code.
>
> In this case, I dont want to run my second.py
> Even I am throwing error from my test.py, but still second.py is getting
> executed, which i dont want,

You must expilicitly ask your shell to do exit if something fails.  Like this:

~~~~~ shellTest.sh ~~~~~~~
#!/bin/bash
set -e
python test.py
python second.py
~~~~~~~~~~~~~~~~~~~~~~~

I explicitly set the shell to /bin/bash on the shebang line, and then
set the -e option to fail when a command exits with a non-zero status.

You should always have a shebang line in your shell files, and execute
them as ./shellTest.sh  (after chmod +x shellTest.sh); moreover, one
for Python files is recommended, like this: #!/usr/bin/env python

-- 
Chris “Kwpolska” Warrick <http://chriswarrick.com/>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense


More information about the Tutor mailing list