Guide to using python for bash-style scripting

4

4zumanga

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
 
4

4zumanga

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.
 
B

Bill Pursell

4zumanga said:
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?
 
J

James Stroud

4zumanga said:
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/
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top