[Tutor] how to do "bash here"

Alan Gauld alan.gauld at blueyonder.co.uk
Mon Nov 17 05:15:08 EST 2003


> I try to learn python by writing working bash-scripts in python. How
can I do
> a bash-syntax like this with python:

Thats probably a mistake.
Bash is a command shell designed to launch other programs
and combine their outputs. Python is a general purpose
programming language. While it is possible to do what
bash does in Python it will be much harder and teach
you very little about Python.

> mysql --host=$MYSQLHOST -A --user=$MYSQLUSER --password=$MYSQLPWD
$MYSQLDB
> <<END_OF_SQLINPUT | sed '1,/^export/d' > "$FDIMPORT"
> $MYSQLEXPORTCMDFD
> END_OF_SQLINPUT

This is a good example of bash calling external programs(mysql & sed)
but there is actually no bash program as such here at all.

If you really really want you can use Python's os module with
its popen function to call mysql too. You could use Pythons
string operations to replace the sed bit.

But there is very little point! The end result would be longer
and probably slower than the bash version. Use bash for what
it is good at and Python for what it is good at.

I think you would be better off going through some of the
tutorials on the Python website.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list