Suggestions on writing a sh <--> python Howto/Tutorial
robert.joeblog at gmail.com
Wed Jul 27 16:25:00 EDT 2011
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"
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 rtfm.py script that does the same thus:
import os, urllib.parse, gzip, subprocess
manfile = subprocess.getoutput("man --path "+PAGE+" "+MAN)
p1 = subprocess.Popen(["gunzip","--to-stdout",manfile],
p2 = subprocess.Popen(["groff", "-Thtml", "-mandoc"], stdin=p1.stdout,
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
f=gzip.open(manfile) 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
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.
More information about the Python-list