[Tutor] how to do "bash here"
dyoo at hkn.eecs.berkeley.edu
Mon Nov 17 01:05:30 EST 2003
On Mon, 17 Nov 2003, Al Bogner wrote:
> I try to learn python by writing working bash-scripts in python. How can
> I do a bash-syntax like this with python:
> mysql --host=$MYSQLHOST -A --user=$MYSQLUSER --password=$MYSQLPWD $MYSQLDB
> <<END_OF_SQLINPUT | sed '1,/^export/d' > "$FDIMPORT"
Hmmm, nice shell command! One thing you may need to fix, though, is the
variable interpolation: passwords, too, can potentially contain spaces, so
you need to protect those shell variables with quotes, just like how you
Anyway, you may find the os.popen() command useful to simulate the "pipes"
that the Unix shell gives us:
And that should take care of it. But instead of doing a literal
translation, it might be more educational to take advantage of the tools
that Python provides. Otherwise, you'll probably only learn how to use
os.popen() well. *grin*
For example, you can use the 'MySQLdb' module to talk to your database,
and the 're' module to perform that regular expression substitution. You
can find out more about doing database manipulation with:
and there's a nice tutorial by A.M. Kuchling for regular expressions:
If you have more questions, please feel free to ask. Good luck to you!
More information about the Tutor