471,052 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,052 software developers and data experts.

Mixing Python And Bash

I just wrote a Python script that is going to be called from bash script.
If the Python script fails, I want the bash script to also stop running.
Unfortunately, I can't seem to get that to work. Here's the script so
far:

# custom python script
/usr/bin/python /home/username/Dev/Python/packZODB.py \
-s"gsgmc.sigma.zettai.net" -b 7

# next program
/usr/bin/someOtherProgram

I don't want the second program to run if the first program fails. Isn't
that the default in bash? If someone could please point me in the right
direction regarding this problem, I would really appreciate it.

Thanks in advance!

Tom Purl

Jul 18 '05 #1
4 3136
Tom Purl <to*@tompurl.com> writes:
I just wrote a Python script that is going to be called from bash script.
If the Python script fails, I want the bash script to also stop running.
Unfortunately, I can't seem to get that to work. Here's the script so
far:

# custom python script
/usr/bin/python /home/username/Dev/Python/packZODB.py \
-s"gsgmc.sigma.zettai.net" -b 7

# next program
/usr/bin/someOtherProgram

I don't want the second program to run if the first program fails. Isn't
that the default in bash? If someone could please point me in the right
direction regarding this problem, I would really appreciate it.


No, that's not the default in bash.

Try:

# custom python script
/usr/bin/python /home/username/Dev/Python/packZODB.py \
-s"gsgmc.sigma.zettai.net" -b 7

if [ $? -eq 0 ]
then
# next program
/usr/bin/someOtherProgram
fi

[Yes, I know there are shorter solutions. But his command line is long
enough as it is.]

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #2
Tom Purl wrote:
I just wrote a Python script that is going to be called from bash script.
If the Python script fails, I want the bash script to also stop running.
Unfortunately, I can't seem to get that to work. Here's the script so
far:

# custom python script
/usr/bin/python /home/username/Dev/Python/packZODB.py \
-s"gsgmc.sigma.zettai.net" -b 7

# next program
/usr/bin/someOtherProgram


Have your Python script return a non-zero return code (e.g. sys.exit(1))
when it fails and then have your shell script check for that:

# custom python script
/usr/bin/python /home/username/Dev/Python/packZODB.py \
-s"gsgmc.sigma.zettai.net" -b 7

if [ $? -ne 0 ]; then
exit 1
fi

# next program
/usr/bin/someOtherProgram
Jul 18 '05 #3
* Tom Purl (2004-11-08 21:17 +0100)
I just wrote a Python script that is going to be called from bash script.
If the Python script fails, I want the bash script to also stop running.
Unfortunately, I can't seem to get that to work. Here's the script so
far:

# custom python script
/usr/bin/python /home/username/Dev/Python/packZODB.py \
-s"gsgmc.sigma.zettai.net" -b 7

# next program
/usr/bin/someOtherProgram

I don't want the second program to run if the first program fails. Isn't
that the default in bash? If someone could please point me in the right
direction regarding this problem, I would really appreciate it.


Hm, not Python but bash related:

if ~/Dev/Python/packZODB.py -s"gsgmc.sigma.zettai.net" -b 7; then
someOtherProgram; fi
Jul 18 '05 #4
Tom Purl a écrit :
I just wrote a Python script that is going to be called from bash script.
If the Python script fails, I want the bash script to also stop running.
Unfortunately, I can't seem to get that to work. Here's the script so
far:

# custom python script
/usr/bin/python /home/username/Dev/Python/packZODB.py \
-s"gsgmc.sigma.zettai.net" -b 7

# next program
/usr/bin/someOtherProgram

I don't want the second program to run if the first program fails. Isn't
that the default in bash? If someone could please point me in the right
direction regarding this problem, I would really appreciate it.

Thanks in advance!

Tom Purl


If you only have 2 programs (or few programs) you can use :

prog1 && prog2

Then prog2 will only be launched if prog1 succeeded ...

Pierre
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jorgen Grahn | last post: by
14 posts views Thread by Rochester | last post: by
16 posts views Thread by John Salerno | last post: by
7 posts views Thread by Frank Potter | last post: by
6 posts views Thread by Ishpeck | last post: by
4 posts views Thread by Stephen Cattaneo | last post: by
6 posts views Thread by Frantisek Malina | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.