[Tutor] Invoking bash from python

Alan Gauld alan.gauld at freenet.co.uk
Mon Aug 7 22:58:15 CEST 2006


>I was thinking that Python would have an expect-like clone and they 
>do:
> http://pexpect.sourceforge.net/
>
> Don't know how good it is though.

Pretty reasonable, its been around for a while.

But if you know the order of requests expect is overkill. Expect is
really for those situations where you have a dialog with the app
and don't know which of many possible prompts you will get back.
Its a bit like awk for streams

but if you know exactly what you will be asked for and when
then simple popen functionality is perfectly adequate and
much simpler to code.

PS.
As an intermediate you can use the telnet module which
allow simple checks for prompts ala expect but doesn't
have all the timer/timeout richness of expect.

Alan G.

>
> On 8/7/06, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
>>
>> >I think I found a solution to my problem and thats to use the os
>> >module (
>> > os.system function). I'm still open to any suggestions you may 
>> > have.
>>
>> os system() will just run the bash program. Not too much avanytage.
>> If you want to control the program from Python you need the popen
>> functions - or the new Subprocess module.
>>
>> Alan G.
>>
>> >
>> > --Will
>> >
>> > On 8/7/06, William Mhlanga <reddazz at gmail.com> wrote:
>> >>
>> >> I'd like to write an app that helps a user install a program on
>> >> Linux
>> >> after fetching it from a remote site. The program is packaged as 
>> >> a
>> >> bin file
>> >> and when run from bash, the user has to agree to a licence
>> >> agreement by
>> >> entering "y" or "n" and enter a path for installation. It seems
>> >> like I need
>> >> to find a way for my program to invoke bash so that the user can
>> >> enter the
>> >> input required. So far, I've figured out that I can use urllib 
>> >> to
>> >> fetch the
>> >> file and the commands module to execute some system commands. 
>> >> Any
>> >> ideas on
>> >> how I can envoke bash and pass on the users input to bash.
>> >>
>> >> Thanks for your input.
>> >>
>> >> Will
>> >>
>> >
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
> 



More information about the Tutor mailing list