views:

49

answers:

2

It works great when I use expect interactively like this:

root@vagrantup:/home/vagrant> expect -i
expect1.1> spawn screen -x
spawn screen -x
3195
expect1.2> send "ls\n"
expect1.3> exit

But the same commands used in a file:

spawn screen -x
send "ls\n"
exit

Only works sometimes, eg. 1/10 time every time I execute the file. It's really weird.

I execute the file with this command:

expect -f Expectfile

I have also tested these:

expect -b Expectfile
expect Expectfile

But I get the same error.

It doesn't say anything. The other screen (since I'm using "screen" command) only responds sometime. Anyone knows why?

+1  A: 

Why? Because you're mis-using Expect.

I'm unconvinced that a stackoverflow thread is an effective medium for teaching what you're missing about Expect. A better starting point is likely to be this: why do you think you should use Expect? To automate screen as you appear to be attempting, you might be better served to use core Tcl, rather than Tcl's Expect extension.

screen is great, Expect is great, and I'm always in favor of automation. The particular combination you've presented sounds more complicated than is likely to be warranted.

Cameron Laird
+3  A: 

I hope you're using ls to stand in for something else, because if you just want a local listing of files, you're far better off using the glob command. Of course, you might actually be doing something like that, what with the use of screen -x too, but it smacks to me of being a little over-complex right now.

Possible failures (though I'm shooting in the dark here due to lack of evidence) could be an interaction between the various things using virtual terminals (both expect and screen do that, and there's just not that many virtual terminals available system-wide) or some kind of timing problem due to not enough time for a connection to a session to be reestablished. (Failing 9 times out of 10 though? That's a lot.) If it is the timing problem, inserting an after 500 will help. If it is the count of terminals, you've got problems; maybe it would be possible to spot this with a tool like lsof but there's a lot of moving parts and I don't know what's failing.

Donal Fellows