1821399 Members
3060 Online
109633 Solutions
New Discussion юеВ

expect help

 
varap
Occasional Advisor

expect help

Hi ,

I have an expect program in which I am trying to timeout after sometime, but my program is coming out before the timeout happens.

Here is the program :-
*********************

#! /usr/local/bin/expect

proc spawn_proc {args} {
global temp_id
puts "\t$args"
eval spawn $args
expect {
timeout { send_user "\nTimed out\n" }
}
return $spawn_id
}

# The program should wait for 100 secs and #then timeout
set timeout 100
spawn_proc ls

puts "program completed successfully "
# program end


****************************************
But with a small modification in the above programs it works as expected.

Modified program :-
*********************

#! /usr/local/bin/expect

proc spawn_proc {args} {
global temp_id
puts "\t$args"
eval spawn $args
return $spawn_id
}

# The program should wait for 100 secs and #then timeout

set timeout 100
spawn_proc ls

# expect statement is taken off in the #spawn_proc and kept here .
expect {
timeout { send_user "\nTimed out\n" }
}

puts "program completed successfully "

# program end

***********************************************

One more difference in the outputs of the above two programs is In the 1st case ( non-timedout case) it displays the output of "ls"
command on the screen and exits without
waiting for the timeout to happen . But in second case it doesn't display the output to the screen , but it waits till timeout occurs .

Can somebody explain me the reasons for these behaviours ???

TIA,
vara.


7 REPLIES 7
Dave Johnson_1
Super Advisor

Re: expect help

What are you trying to accomplish with this? I assume you want the output of the ls command to be displayed to the screen. The idea of the timeout is to be able to report an error if you do not get the expected results in some period of time. Your expect statement is only looking for a timeout, you have no response you are looking for such as the shell prompt.

I am new to expect as well, but I do have a couple of working scripts so I might be able to help once I understand what you really want to do.

-Dave
Dave Johnson_1
Super Advisor

Re: expect help

What are you trying to accomplish with this? I assume you want the output of the ls command to be displayed to the screen. The idea of the timeout is to be able to report an error if you do not get the expected results in some period of time. Your expect statement is only looking for a timeout, you have no response you are looking for such as the shell prompt.

I am new to expect as well, but I do have a couple of working scripts so I might be able to help once I understand what you really want to do.

-Dave
varap
Occasional Advisor

Re: expect help

Hi Dave,

I want my program to wait for 100 secs after "ls" is executed . Why is this not
happening in the 1st case ??? As I have not
put any matching patterns in expect , I expected the program to wait for 100 secs
in spawn_proc function . I could able to achieve the expected behaviour in the second
case with similar login .

Just execute the above two scripts . You will
see the difference .

TIA,
vara.
Dave Johnson_1
Super Advisor

Re: expect help

If you want to pause/sleep after executing a command use the sleep statement. In the first example you are look for results with in a given amount of time. Time out is a trigger that fires if no output, or specified output is not found in the timeout period. This is an error tracking not time waisting thing. To waist time after executing a command use the sleep statement.

ex: assume root is running this
eval spawn $args
expect {
timeout { send_user "\nTimed out\n" }
"# "
}
sleep 100

This will expect to get the prompt back within timeout period then sleep for 100 seconds.

Does this help? or do I not understand your question?

-Dave
varap
Occasional Advisor

Re: expect help

Hi Dave ,

I think I didn't specify my requirement clearly . Here I am putting my requirement
exactly .

For simplying the problem I have used "ls"
as an arg to spawn_proc. You have suggested sleep to make it wait for 100secs after the execution of "ls" . But In my actual test script I want to create and delete the files
under some dir ( Its a "while true" loop ).

Here I am pasting that portion of code.
set timeout 300
spawn_proc /opt/ids/test/agent/common/realroot -c "\
mkdir -p /tmp/mydirtowatch;
while true
do
touch /tmp/mydirtowatch/file1;
touch /tmp/mydirtowatch/file2;
touch /tmp/mydirtowatch/file3;
rm /tmp/mydirtowatch/file?;
done"


So , Its is not just waiting . The script should execute the above while loop for 100secs and timeout after that. So I can't
use sleep here.

Let me know If you have any other idea to do this through expect script.

Regds,
Vara





Dave Johnson_1
Super Advisor

Re: expect help

This can be done in the POSIX shell with out expect as:

mkdir -p /tmp/mydirtowatch;
SECONDS=0
while [ $SECONDS -lt 300 ]
do
touch /tmp/mydirtowatch/file1;
touch /tmp/mydirtowatch/file2;
touch /tmp/mydirtowatch/file3;
rm /tmp/mydirtowatch/file?;
done
Paddy_1
Valued Contributor

Re: expect help

try using
"set timeout -a"

The sufficiency of my merit is to know that my merit is NOT sufficient