By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

library to do easy shell scripting in Python

P: n/a
Recently a post that mentioned a recipe that extended subprocess to
allow killable processes caused me to do some thinking. Some of my
larger bash scripts are starting to become a bit unwieldy (hundreds of
lines of code). Yet for many things bash just works out so well because
it is so close to the file system and processes. As part of another
project, I now have need of a really good library to make it almost as
easy to do things in Python as it is in Bash. With a simple wrapper
around subprocess, I'm pretty much able to do most things. Most of my
complicated bash hackery involves using awk, sed, grep, and cut to
process text, which python does quite nicely, thank you very much. But
there's a few things to add.

To wit, I'm wanting to write a library that can deal with the following

- spawn a process, feed it std in, get stdout, stderr, and err code.
This is largely already accomplished by subprocess
- spawn off processes as background daemons
- spawn multiple processes and pipe output to input.
- can do fancier things like bash does, like combine stderr/stdout,
switch stderr/stdout, redirects to and from files
- transparently allow a python function or object to be a part of
the pipeline at any stage.

Questions include, how would one design the interface for things, like
assembling pipes? Several ideas include:




The former doesn't deal very well with re-plumbing of the pipes, nor is
there an easy way to redirect to and from a file. The second syntax is
more flexible but a bit cumbersome. Also it doesn't allow redirection
or flexible plumbing either.

Any ideas on how I could design this?
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
On Apr 24, 12:22 pm, Michael Torrie <torr...@gmail.comwrote:
Any ideas on how I could design this?
There's a recipe on Activestate's Python Cookbook that does pretty
much this:
Allows arbitrary number of commands to be strung together with
each one feeding into the next ones input. Syntax is simple:
x=pipe("cmd1", "cmd2", "cmd3").read() is equivalent to bash
command x=`cmd1 | cmd2 | cmd3`.

I haven't used it myself, but it might make a good place to start.

- alex23

Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.