Mixing Python And Bash

T

Tom Purl

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
 
M

Mike Meyer

Tom Purl said:
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
 
L

Leif K-Brooks

Tom said:
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
 
T

Thorsten Kampe

* 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
 
P

Pierre Barbier de Reuille

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top