Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Need help in looping expect

Posted by dphe (dphe), 4 February 2008
I have a set of regular expressions that are picked from a file and then loaded in to a list.

Now i want to spawn a process and then check for each element in the list for the process output using expect.  How can i do that?

Example :

File.log  contains:


Now i read the above to a list say $checklist

now if i've to spawn like below and check or things in the checklist.. what should i do?

spawn ls

set i 0

set reg [lindex [lindex $checklist $i] 0]

exp_internal 1
            expect {
                       -re $reg {
                                         incr i
                                         set reg [lindex [lindex $checklist $i] 0]
                                         exp_send $reg
                       eof {
                             catch {close -i $spawn_id}
                             wait -nowait -i $spawn_id


what should i do to compare the list variables to the spawned process out.

Please do help me with this..

Thanks a lot in advance...

Posted by admin (Graham Ellis), 5 February 2008
That's a very good question - in fact one that's worth sharing the answer wider.  I've written it up and provided examples on my blog (archived later at with sample programs at


Posted by dphe (dphe), 5 February 2008
Thank you for the reply .  But there is problem with things that you have listed in the example.

Your example works fine in case of a input from the command line.  Thats, perfectly fine.

But when I spawn a process and pass the spawn_id for the expect to pick things from a list and check the same doesn't work.

Tried different ways to get this done, at last succeeded by running a expect inside expect with a return value 4, that allows it to run the loop completely.

If you can provide me with a better solution it would be really appreciated.

Thanks in advance...

Posted by admin (Graham Ellis), 6 February 2008
I'm glad you have a solution.  

I gave two different examples, both of which worked, and I'm not clear which of them you're reporting to fail if you change the source you're expecting from - a failure that seems very odd.   I would think that any problem would be a relatively easy coding issue - I had to do a couple of tests on my examples before they ran; it's rarely possible to write bugfree expect code straight off - at least for me!

Posted by dphe (dphe), 7 February 2008
Thanks again Graham, the problem came in when I tried to use the list against the output from a spawned process and the problem that i came across was:

1: I wanted the expressions in the list matched individually and had report a pass or fail.  ( Here I had the problem looping the same back )

2: By the time expect was trying to compare the out the spawn_id would close, which brings be doing to the timeout block.

Second one, still i'm not able to resolve since it tries the set of expression and later just comes back with spawn id is not open.

Can you please help?

Thanks a lot and I don't think everyone can just write a bugfree and custom code for everyone's desires.  Your code did help a lot, thanks for the same.  

Posted by admin (Graham Ellis), 8 February 2008
I would look at chaning the timeout, and at adding a timeout clause to the expect to trap it when it happens.  Without looking at the specific code, I'm finding it hard to appreciate what exactly is going wrong but you certainly can start by making these changes which will avoid the dafult timeout if it's too short, and will avod your program craching it it has timedout.   At a deeper level though, I wonder if there's an issue with your logic or if the spawned process really DOES take a long time?

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho