Suggestions on writing a sh <--> python Howto/Tutorial


Robert Laing

I've been tinkering around learning traditional Unix shell programming
and python at the same time. I set myself the following exercise which
I found quite educational. I first wrote a shell CGI script to read
the man pages on my web hosting service's computer via a browser like


Where page=<n> is optional since one does not often need it. The shell
script that did the trick looks so:

MAN=$(echo $QUERY_STRING | sed -n "s/^.*man=\([^&]*\).*$/\1/p")
PAGE=$(echo $QUERY_STRING | sed -n "s/^.*page=\([^&]*\).*$/\1/p")
echo "Content-type: text/html; charset=utf-8"
echo ""
gunzip < $(man --path $PAGE $MAN) | groff -Thtml -mandoc

That last line with the < redirect and pipe to groff is not purely
illustrative since while the Linux distro on my laptap has a version
of man that understands -Thtml, the one on my hosting company's server
does not (making the above quite a valuable script).

Now I managed to write a script that does the same thus:

import os, urllib.parse, gzip, subprocess
except (KeyError):
manfile = subprocess.getoutput("man --path "+PAGE+" "+MAN)
p1 = subprocess.Popen(["gunzip","--to-stdout",manfile],
p2 = subprocess.Popen(["groff", "-Thtml", "-mandoc"], stdin=p1.stdout,
stdout=subprocess.PIPE, universal_newlines=True)
print("Content-type: text/html; charset=utf-8\n")

Figuring I needed to add 'universal_newlines=True' to get rid of all
the '\n's took me quite a while.

I'm now modifying this to use python's gzip library rather than
calling gunzip as a subprocess, but so far produce gibberish since insists on flagging the text output as binary
data which confuses groff. That, however, is not the issue here.

The point is, I thought it would be worthwhile writing a Howto/
Tutorial on doing shell script type programming with python, and
thought I'd start a thread for suggestions on tricks from the gurus
out there.

PS: lets avoid any ideological fights about bash vs python. From my
side, I found the bash version of the above quicker and easier to
write, but would hate to expand it into a bigger program, whereas
python got my mind buzzing with ideas on features to add.


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