[FRPythoneers] Shell commands in interactive mode

cary miller cmiller at math.cudenver.edu
Wed Jul 25 13:32:58 MDT 2001


That will get you arbitrary shell commands but what I would like to have
is completely transparent access to the shell without qoute marks or any
python commands intervening.  It's probably a mad dream but fun to think
about.  Here is another idea.

>>> sh cat foo | grep bar > baz.py
for example.
You precede the shell command with sh (an undeclared variable).
You have code that looks for NameError: sh
When such an error is found the rest of the line gets executed with the
command module or something.
I think it would work but it seems a bit fishy so I have not tried it.

cam

On Wed, 25 Jul 2001, Eric Brunson wrote:

>
> Try reading about commands module and see if that does what you need
> it to do.
>
> http://python.org/doc/current/lib/module-commands.html
>
> * cary miller (cmiller at math.cudenver.edu) [010725 13:06]:
> > Hi
> >
> > In tcl interactive mode you have access to shell commands like pwd and
> > ls.  I wanted python to do that and came up with the following.  This
> > code goes in pythonstartup.  The nice thing about it is that you just
> > type
> > >>> ls
> > or
> > >>> pwd
> > and get exactly what you would see if you typed the same in the shell.
> >
> > Problems:
> >     1.  Blank line after the output.  The problem here is __repr__ is
> >     required to return a string.  Is there any way around this?
> >
> >     2.  Only works for one-word commands.  Cannot pass options to ls,
> >     and cannot do this
> >
> > >>> cat foo.py
> >
> >     Is there a way to do multiword shell commands??
> >
> > import os
> >
> > class ShellCommand:
> >     def __init__(self,cmd):
> >         self.cmd=cmd
> >     def __repr__(self):
> >         eval('os.system(self.cmd)')
> >         return ''
> >
> > pwd = ShellCommand('pwd')
> > ls = ShellCommand('ls')
> >
> >
> > Here is the best I aan come up with for cat.
> >
> >
> > def cat(file):
> >     for line in open(file).readlines():
> >         print line,
> >
> >
> >
> > --
> > Cary Miller
> > Math Deparment
> > University of Colorado - Denver
> > cmiller at math.cudenver.edu
> > http://www-math.cudenver.edu/~cmiller
> >
> > _______________________________________________
> > This message sent by the FRPythoneers mailing list.
> > Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> > URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers
>
>
>

-- 
Cary Miller
Math Deparment
University of Colorado - Denver
cmiller at math.cudenver.edu
http://www-math.cudenver.edu/~cmiller




More information about the FRPythoneers mailing list