473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3251
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jorgen Grahn | last post by:
I couldn't think of a good solution, and it's hard to Google for... I write python command-line programs under Win2k, and I use the bash shell from Cygwin. I cannot use Cygwin's python package...
2
by: Eric Woudenberg | last post by:
I just installed a Python 2.3.4 Windows binary on a friend's WinXP machine (because the latest Cygwin-provided Python 2.3 build leaves out the winsound module for some reason). When I try and...
9
by: TPJ | last post by:
First I have to admit that my English isn't good enough. I'm still studying and sometimes I just can't express what I want to express. A few weeks ago I've written 'Python Builder' - a bash...
14
by: Rochester | last post by:
Hi, I just found out that the general open file mechanism doesn't work for named pipes (fifo). Say I wrote something like this and it simply hangs python: #!/usr/bin/python import os
16
by: John Salerno | last post by:
Hi all. I just installed Ubuntu and I'm learning how to use the bash shell. Aside from the normal commands you can use, I was wondering if it's possible to use Python from the terminal instead of...
7
by: Frank Potter | last post by:
I learned some python in windows. And now I've turned to linux. I read a book and it teaches how to write shell script with bash, but I don't feel like the grammar of bash. Since I know about...
6
by: Ishpeck | last post by:
I'm using Python to automate testing software for my company. I wanted the computers in my testing lab to automatically fetch the latest version of the python scripts from a CVS repository and...
4
by: Stephen Cattaneo | last post by:
Hello all, I am attempting to execute an automated test (written in Python) via cron. I have to check the HOSTNAME variable as part of the test, oddly under cron the HOSTNAME environment...
6
by: Frantisek Malina | last post by:
What is the best way to do the regular bash commands in native python? - create directory - create file - make a symlink - copy a file to another directory - move a file - set permissions ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.