473,385 Members | 1,693 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,385 software developers and data experts.

Guide to using python for bash-style scripting

I have a bunch of really horrible hacked-up bash scripts which I would
really like to convert to python, so I can extend and neaten them.
However, I'm having some trouble mapping some constructs easily, and
was wondering if anyone know of a guide to mapping simple uses of
command line programs to python.

For an example, the kind of thing I am thinking of are things like
(yes, this is horrible code).

# These are a run of a program I have written
../proggy -test1 > out1
../proggy -test2 > out2

#Do some simple manipulation of the output.
grep Node out1 > new_out1
grep Node out2 > new_out2
diff out1 out2

May 23 '06 #1
4 1867
Yes, there is a stupid mistake in that script, last line should be:

diff new_out1 new_out2

However, this is hopefully not important, what is important is the
general kind of (very simple) things I'm trying to do.

May 23 '06 #2

4zumanga wrote:
Yes, there is a stupid mistake in that script, last line should be:

diff new_out1 new_out2

However, this is hopefully not important, what is important is the
general kind of (very simple) things I'm trying to do.


I have been hoping for a good solution to this. An
easy way to handle simple commands is:

#!/usr/bin/env python

import os
s = "echo foo | sed 's/foo/gap/' > file\n"
s += "wc -c file\n"
s += "cat file\n"

print s, "***"
os.system(s)
######## end

(Apologies for the lameness of the commands above).
However, I have some bash scripts that rely on
things like PIPESTATUS, and I have no idea
how to emulate that behavior easily. How can one
most easily emulate a simple pipe as readily
as in bash? I've seen a few recipes for doing something
like that, but I haven't yet seen one that i really
like. Is it possible to execute:
os.system( " a | b | c | d | e")
and retrieve the value of PIPESTATUS?

May 23 '06 #3

python subprocess module docs:

http://docs.python.org/dev/lib/node517.html

May 23 '06 #4
4zumanga wrote:
I have a bunch of really horrible hacked-up bash scripts which I would
really like to convert to python, so I can extend and neaten them.
However, I'm having some trouble mapping some constructs easily, and
was wondering if anyone know of a guide to mapping simple uses of
command line programs to python.

For an example, the kind of thing I am thinking of are things like
(yes, this is horrible code).

# These are a run of a program I have written
./proggy -test1 > out1
./proggy -test2 > out2

#Do some simple manipulation of the output.
grep Node out1 > new_out1
grep Node out2 > new_out2
diff out1 out2


Chapter 2 of Mark Lutz's "Programming Python" (2nd Edition) will make
you adept at using Python as a (***VASTLY SUPERIOR***) alternative to
shell scripting. Chapters 3-5 will get you pretty close to master level
at shell/system scripting.

Note: This is not an intro to python. You should already be able to do
simple programming in python for Lutz's book to be useful.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
May 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Robin Siebler | last post by:
I have a bunch of command line tests that I need to automate. I've run into too many bugs with the default Win2k command shell, so I need to either purchase 4NT or handle the logic and output...
4
by: Tom Purl | last post by:
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. ...
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...
2
by: Foobarius Frobinium | last post by:
http://thelinuxlink.net/~fingolfin/pointer-guide/ I've made a lot of corrections, and put in more new stuff. I have malloc and pointers to struct's and maybe a few more things and I'm done. ...
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...
12
by: Alex | last post by:
Hi all, The subject says pretty much all, i would very appreciate an answer. I tried to search the various forums and groups, but didn't find any specific answer... Thanks, Alex.
5
by: aha | last post by:
Hello All, I have a situation where I can count on a Python installation being available on a system, but I can't count on it being a version of Python needed by my application. Since my...
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...
0
by: Cameron Simpson | last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote: Because $HOSTNAME is a bash specific variable, set by bash but NOT EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.