Two things:Why does the list have a set of empty brackets at the end which foreach sees as an element?Why does foreach move on to the next item in the list before it has executed all the commands in the loop?

expect version 5.42.1tcl-8.4.7-2RHEL 4U4

I ran this with a slightly newer version of expect and tcl but had the same result.

I am TCL newbie and I thought I had copied all the best code on the Internet but it is not working for me

Why does the list have a set of empty brackets at the end which foreach sees as an element?

Why does foreach move on to the next item in the list before it has executed all the commands in the loop?

Expect code is notoriorsly dependent on the system on which it is running as you're using it to paste a huge number of utilities together each of which may vary a little in how it works - really subtle changes. You've got a huge pile of bits of software, each of which is good in its own right, but as you add them on of of each other and combine them, you'll find that the very top layer that ties them all together has to be adjusted to work. I'm not surprised that the code doesn't just work for you, and the best way to correct that is to investigate and understand the detail of what's going and fix the problems.

The empty element is probably an empty line at the end of your file.

Spawn starts another process ... so even while your spawned command is still running, your foreach loop can proceed - it depends on just what you have in your "do some other stuff".

I am concerned at the apparent lack of access to ssh - although you don't comment, I think this might be an underlying problem.

Posted by cwjolly (cwjolly), 24 August 2007

Why does the list have a set of empty brackets at the end which foreach sees as an element? This means that your file has a \n as the last character. For instance ifyour string is a\nb you will get {a b } as your list when you split. If yourstring is a\nb\n you will get { a b {} } . Why does foreach move on to the next item in the list before it has executed all the commands in the loop? From the error you get and the code you've provided you have not shown us how the expect call is being made. There is nothing in the spawn command that says stop and wait. All it does is fork a child, exec your program and sets up the pipes between your script and the child andreturns without blocking. So you are free to ignore what spawn has given you or you can interact with it. In this case ( at least what you show) is you never call expect in the loop so you just spawning a bunch of processes and then exiting.

Some comments since you have not included all your code. From the error message you are using puts inside of a call to expect ... always use send_user. You call stty -echo but never call the counterpart to restore echoing stty echo this should be done afte getting the password. Your code cannot find the ssh executable. You should check for this by using :set SSH [auto_execok ssh ]if { [ string length $SSH] == 0 } { puts "Unable to find ssh; Make sure your PATH variable has the directory where ssh is located in it" exit 1}

then use spawn $SSH $host I you have alot of hosts you will eventually be unable to spawn anymore unless you exp_wait then exp_close. Here is a template that you can use in your scripts to always write your expect loops.