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.
Expect - how to spawn a file read operation

Posted by neilvp (neilvp), 1 September 2006
Oh Great and Glorious Guru Graham your assistance please...

My challenge this week is to run a TCL script that waits for a router to boot, then tells it to multiple ping tests, collects the results, and then reboots router, repeat ad-infinitum.

This will be done over a serial port.

I have created a text file from the terminal interaction (i.e. having done it manually) and intend to use this as the source for testing the script before running live. i.e. read a line from this text file and use that as if it had come from the serial port.

The question is, how do I spawn this 'read a line from the file' so that Expect can use it?
..or should I try something different

I am running the current ActiveState releases under Windoze XP.

Your hints, tips, help and assistance, is, as ever, very much appreciated.


Posted by admin (Graham Ellis), 1 September 2006
On unix / linux,I would spawn a cat command.  Using the same logic on XP, I guess you would spawn a type command; not sure if that logic's going to work, but at least it's somewhere straightforward to start trying it out and, after all, you are only looking for a solution for testing purposes  

Posted by neilvp (neilvp), 1 September 2006
Thanks Graham.

This did not work but having caught the error I am puzzled as to the real reason...

Unfortunately I cannot figure out how to attach a screen shot to this mail ...

As you can see from the code, prior to my trying to spawn I check that the file exists, why do I do this (apart from good programming of course) is because the reason that is reported for the spawn failing is  'The system cannot find the file specified'

However, the file does exist in the same pwd and the 'file exists ' check supports this.

Any ideas as to why the 'spawn type' cannot find the file?



package require Expect

set FileToRead test_file_1.txt

puts "File To Read is $FileToRead\r"

if { [file exists $FileToRead] } \
   puts "$FileToRead exists\r"
} else \
   puts "$FileToRead does not exist\r"

if [catch {spawn type $FileToRead}  reason]\
   puts "Failed to spawn\r"
   puts "Reason $reason\r"
} else {
   puts "spawned"    
  puts "Ending Program"

Posted by neilvp (neilvp), 1 September 2006

It is of course the 'TYPE.EXE' it cannot find. To make matters worse type.exe (or no longer exists as a standalone program within XP, it is, I am told, built into the DOS shell which is why that test worked and I consequently thought the spawn could not find the target file.

Any other suggestions?
(Running cygwin is not an option - sorry)


Posted by neilvp (neilvp), 1 September 2006
Courtesy of those very nice people at sourceforge I found a cat utility for Windoze.

That certainly did what I thought I wanted but it has given me other problems to solve now... like how can I get it to do it one line at a time (or get the expect to loop)?

Can I do something like

while {stdout <> eof} {
expect {
              test1  { action1}
              test2  {action2}

And I thought it such a straight forward task



Posted by admin (Graham Ellis), 1 September 2006
You can do

while 1 {
     expect ...


and have an eof option on your expect.

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