473,606 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.z ettai.net" -b 7

# next program
/usr/bin/someOtherProgra m

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 3266
Tom Purl <to*@tompurl.co m> 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.z ettai.net" -b 7

# next program
/usr/bin/someOtherProgra m

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.z ettai.net" -b 7

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

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

<mike
--
Mike Meyer <mw*@mired.or g> 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.z ettai.net" -b 7

# next program
/usr/bin/someOtherProgra m


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.z ettai.net" -b 7

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

# next program
/usr/bin/someOtherProgra m
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.z ettai.net" -b 7

# next program
/usr/bin/someOtherProgra m

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.z ettai.net" -b 7; then
someOtherProgra m; 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.z ettai.net" -b 7

# next program
/usr/bin/someOtherProgra m

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
14348
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 because of a binary module which has to be compiled with Visual C 6. My scripts start with a '#!/usr/bin/env python' shebang, as God intended. Now, I assume I can make cmd.exe run foo.py by asociating *.py with the python interpreter.
2
2976
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 run this newly installed Python from bash it just hangs (no version message, no prompt, CPU is idle). When I run it from IDLE, or the DOS CMD prompt, it runs fine. Also, Python scripts using the new version run fine, even invoked on the bash command...
9
2070
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 script that allows anyone to download, compile (with flags given by user) and install Python and some external modules (e.g. wxPython, PyGTK, Numeric...). I use Python every day and when new version of Python (or some external module) is released, I...
14
19733
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
3931
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 the normal bash commands (e.g. print instead of echo). My main reason for asking is that I like using Python for everything, and if I don't need to learn the bash 'language', then I won't just yet. Thanks.
7
2236
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 python, I want to get a linux shell which use python grammar. I searched by google and I found pysh, which is not maintained any more. There's another script named pyshell, which is not likely what I'm searching for.
6
3940
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 then ask a local database server which of the scripts to run. I built the following: #!/bin/bash # Batcher will run the specified scripts.
4
7510
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 variable is not in the os.environ dictionary. I know that cron runs in a subshell that does not have all of the normally set environment variables. HOSTNAME is not one of those variables, it is set even in cron's subshell. Why doesn't python get...
6
1866
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 I need to write a program that creates real application/FTP accounts
0
7939
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8428
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8078
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6753
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3919
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2442
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1285
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.