[Tutor] how to do "bash here"

Danny Yoo 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"
> $MYSQLEXPORTCMDFD
> END_OF_SQLINPUT

Hi Al,

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
protected $FDIMPORT.


Anyway, you may find the os.popen() command useful to simulate the "pipes"
that the Unix shell gives us:

    http://www.python.org/doc/lib/os-newstreams.html#l2h-1379

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:

    http://www.devshed.com/Server_Side/Python/PythonMySQL/print_html

and there's a nice tutorial by A.M. Kuchling for regular expressions:

    http://www.amk.ca/python/howto/regex/


If you have more questions, please feel free to ask.  Good luck to you!




More information about the Tutor mailing list