-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect.man
2615 lines (2421 loc) · 77.4 KB
/
expect.man
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.TH EXPECT 1 "29 December 1994"
.SH NAME
expect \- programmed dialogue with interactive programs, Version 5
.SH SYNOPSIS
.B expect
[
.B \-dDinN
]
[
.B \-c
.I cmds
]
[
[
.BR \- [ f | b ]
]
.I cmdfile
]
[
.I args
]
.SH INTRODUCTION
.B Expect
is a program that "talks" to other interactive programs according to a
script. Following the script,
.B Expect
knows what can be expected from
a program and what the correct response should be. An interpreted
language provides branching and high-level control structures to
direct the dialogue. In addition, the user can take control
and interact directly when desired, afterward returning control to the
script.
.PP
.B Expectk
is a mixture of
.B Expect
and
.BR Tk .
It behaves just like
.B Expect
and
.BR Tk 's
.BR wish .
.B Expect
can also be used directly in C or C++ (that is, without Tcl).
See libexpect(3).
.PP
The name "Expect" comes from the idea of
.I send/expect
sequences popularized
by uucp, kermit and other modem control programs.
However unlike uucp,
.B Expect
is generalized so that it can be run as a user-level command
with any program and task in mind.
.B Expect
can actually talk to several programs at the same time.
.PP
For example, here are some things
.B Expect
can do:
.RS
.TP 4
\(bu
Cause your computer to dial you back,
so that you can login without paying for the call.
.TP
\(bu
Start a game (e.g., rogue) and if the optimal configuration doesn't appear,
restart it (again and again) until it does,
then hand over control to you.
.TP
\(bu
Run fsck, and in response to its questions, answer "yes", "no" or give control back to you,
based on predetermined criteria.
.TP
\(bu
Connect to another network or BBS (e.g., MCI Mail, CompuServe) and
automatically retrieve your mail so that it appears as if
it was originally sent to your local system.
.TP
\(bu
Carry environment variables, current directory,
or any kind of information across rlogin, telnet, tip, su, chgrp, etc.
.RE
.PP
There are a variety of reasons why the shell cannot perform these tasks.
(Try, you'll see.)
All are possible with
.BR Expect .
.PP
In general,
.B Expect
is useful for running any program which requires
interaction between the program and the user.
All that is necessary is that the interaction can be characterized
programmatically.
.B Expect
can also give the user back control
(without halting the program being controlled) if desired.
Similarly, the user can return control to the script at any time.
.SH USAGE
.B Expect
reads
.I cmdfile
for a list of commands to execute.
.B Expect
may also be invoked implicitly on systems which support the #! notation
by marking the script executable, and making the first line in your script:
#!/usr/local/bin/expect \-f
Of course, the path must accurately describe where
.B Expect
lives. /usr/local/bin is just an example.
The
.B \-c
flag prefaces a command to be executed before any in the script.
The command should be quoted to prevent being broken up by the shell.
This option may be used multiple times.
Multiple commands may be
executed with a single
.B \-c
by separating them with semicolons.
Commands are executed in the order they appear.
(When using Expectk, this option is specified as
.BR \-command .)
.PP
The
.B \-d
flag enables some diagnostic output, which
primarily reports internal activity of commands such as
.B expect
and
.BR interact .
This flag has the same effect as "exp_internal 1" at the beginning of an Expect
script, plus the version of
.B Expect
is printed.
(The
.B strace
command is useful for tracing statements, and the
.B trace
command is useful for tracing variable assignments.)
(When using Expectk, this option is specified as
.BR \-diag .)
.PP
The
.B \-D
flag enables an interactive debugger. An integer value should follow.
The debugger will take control before the next Tcl procedure
if the value is non-zero
or if a ^C is pressed (or a breakpoint is hit, or other appropriate debugger
command appears in the script). See the README file or SEE ALSO (below)
for more information on the debugger.
(When using Expectk, this option is specified as
.BR \-Debug .)
.PP
The
.B \-f
flag prefaces a file from which to read commands from.
The flag itself is optional as it is only useful when using
the #! notation (see above),
so that other arguments may be supplied on the command line.
(When using Expectk, this option is specified as
.BR \-file .)
.PP
By default, the command file is read into memory and executed in its entirety.
It is occasionally desirable to read files one line at a time. For example,
stdin is read this way. In order to force arbitrary files to be handled this
way, use the
.B \-b
flag.
(When using Expectk, this option is specified as
.BR \-buffer .)
Note that stdio-buffering may still take place however this shouldn't cause
problems when reading from a fifo or stdin.
.PP
If the string "\-" is supplied as a filename, standard input is read instead.
(Use "./\-" to read from a file actually named "\-".)
.PP
The
.B \-i
flag causes
.B Expect
to interactively prompt for commands instead of reading
them from a file.
Prompting is terminated via the
.B exit
command or upon EOF.
See
.B interpreter
(below) for more information.
.B \-i
is assumed if neither a command file nor
.B \-c
is used.
(When using Expectk, this option is specified as
.BR \-interactive .)
.PP
.B \-\-
may be used to delimit the end of the options. This is useful if
you want to pass an option-like argument to your script without it being
interpreted by
.BR Expect .
This can usefully be placed in the #! line to prevent any flag-like
interpretation by Expect. For example, the following will leave the
original arguments (including the script name) in the variable
.IR argv .
#!/usr/local/bin/expect \-\-
Note that the usual getopt(3) and execve(2) conventions must be observed
when adding arguments to the #! line.
.PP
The file $exp_library/expect.rc is sourced automatically if present, unless
the
.B \-N
flag is used.
(When using Expectk, this option is specified as
.BR \-NORC .)
Immediately after this,
the file ~/.expect.rc is sourced automatically, unless the
.B \-n
flag is used. If the environment variable DOTDIR is defined,
it is treated as a directory and .expect.rc is read from there.
(When using Expectk, this option is specified as
.BR \-norc .)
This sourcing occurs only after executing any
.B \-c
flags.
.PP
.B \-v
causes Expect to print its version number and exit. (The corresponding flag
in Expectk, which uses long flag names, is \-version.)
.PP
Optional
.I args
are constructed into a list and stored in the variable named
.IR argv .
.I argc
is initialized to the length of argv.
.PP
.I argv0
is defined to be the name of the script (or binary if no script is used).
For example,
the following prints out the name of the script and the first three arguments:
.nf
send_user "$argv0 [lrange $argv 0 2]\\n"
.fi
.SH COMMANDS
.B Expect
uses
.I Tcl
(Tool Command Language).
Tcl provides control flow (e.g., if, for, break),
expression evaluation and several other features such as recursion,
procedure definition, etc.
Commands used here but not defined (e.g.,
.BR set ,
.BR if ,
.BR exec )
are Tcl commands (see tcl(3)).
.B Expect
supports additional commands, described below.
Unless otherwise specified, commands return the empty string.
.PP
Commands are listed alphabetically so that they can be quickly located.
However, new users may find it easier to start by reading the descriptions
of
.BR spawn ,
.BR send ,
.BR expect ,
and
.BR interact ,
in that order.
Note that the best introduction to the language (both Expect and Tcl)
is provided in the book "Exploring Expect" (see SEE ALSO below).
Examples are included in this man page but they are very limited since
this man page is meant primarily as reference material.
Note that in the text of this man page, "Expect" with an uppercase "E"
refers to the
.B Expect
program while "expect" with a lower-case "e" refers to the
.B expect
command within the
.B Expect
program.)
.I
.TP 6
.BI close " [-slave] [\-onexec 0|1] [\-i spawn_id]"
closes the connection to the current process.
Most interactive programs will detect EOF on their stdin and exit;
thus
.B close
usually suffices to kill the process as well.
The
.B \-i
flag declares the process to close corresponding to the named spawn_id.
Both
.B expect
and
.B interact
will detect when the current process exits and implicitly do a
.BR close .
But if you kill the process by, say, "exec kill $pid",
you will need to explicitly call
.BR close .
The
.BR \-onexec
flag determines whether the spawn id will be closed in any new spawned
processes or if the process is overlayed. To leave a spawn id open,
use the value 0. A non-zero integer value will force the spawn closed
(the default) in any new processes.
The
.B \-slave
flag closes the slave associated with the spawn id. (See "spawn -pty".)
When the connection is closed, the slave is automatically closed as
well if still open.
No matter whether the connection is closed implicitly or explicitly,
you should call
.B wait
to clear up the corresponding kernel process slot.
.B close
does not call
.B wait
since there is no guarantee that closing a process connection will cause
it to exit.
See
.B wait
below for more info.
.TP
.BI debug " [[-now] 0|1]"
controls a Tcl debugger allowing you to step through statements, set
breakpoints, etc.
With no arguments, a 1 is returned if the debugger is not running, otherwise
a 0 is returned.
With a 1 argument, the debugger is started. With a 0 argument, the
debugger is stopped. If a 1 argument is preceded by the
.B \-now
flag, the debugger is started immediately (i.e., in the middle of the
.B debug
command itself). Otherwise, the debugger is started with the next
Tcl statement.
The
.B debug
command does not change any traps. Compare this to starting Expect with the
.B -D
flag (see above).
See the README file or SEE ALSO (below)
for more information on the debugger.
.TP
.B disconnect
disconnects a forked process from the terminal. It continues running in the
background. The process is given its own process group (if possible).
Standard I/O is redirected to /dev/null.
.IP
The following fragment uses
.B disconnect
to continue running the script in the background.
.nf
if {[fork]!=0} exit
disconnect
. . .
.fi
The following script reads a password, and then runs a program
every hour that demands a password each time it is run. The script supplies
the password so that you only have to type it once.
(See the
.B stty
command which demonstrates how to turn off password echoing.)
.nf
send_user "password?\\ "
expect_user -re "(.*)\\n"
for {} 1 {} {
if {[fork]!=0} {sleep 3600;continue}
disconnect
spawn priv_prog
expect Password:
send "$expect_out(1,string)\\r"
. . .
exit
}
.fi
An advantage to using
.B disconnect
over the shell asynchronous process feature (&) is that
.B Expect
can
save the terminal parameters prior to disconnection, and then later
apply them to new ptys. With &,
.B Expect
does not have a chance
to read the terminal's parameters since the terminal is already
disconnected by the time
.B Expect
receives control.
.TP
.BI exit " [\-opts] [status]"
causes
.B Expect
to exit or otherwise prepare to do so.
The
.B \-onexit
flag causes the next argument to be used as an exit handler.
Without an argument, the current exit handler is returned.
The
.B \-noexit
flag causes
.B Expect
to prepare to exit but stop short of actually returning control to the
operating system. The user-defined exit handler is run as well as Expect's
own internal handlers.
No further Expect commands should be executed.
This is useful if you are running Expect with other Tcl extensions.
The current interpreter (and main window if in the Tk environment) remain
so that other Tcl extensions can clean up. If Expect's
.B exit
is called again (however this might occur), the handlers are not rerun.
Upon exiting,
all connections to spawned processes are closed. Closure will be detected
as an EOF by spawned processes.
.B exit
takes no other actions beyond what the normal _exit(2) procedure does.
Thus, spawned processes that do not check for EOF may continue to run.
(A variety of conditions are important to determining, for example, what
signals a spawned process will be sent, but these are system-dependent,
typically documented under exit(3).)
Spawned processes that continue to run will be inherited by init.
.I status
(or 0 if not specified) is returned as the exit status of
.BR Expect .
.B exit
is implicitly executed if the end of the script is reached.
.TP
\fBexp_continue\fR [-continue_timer]
The command
.B exp_continue
allows
.B expect
itself to continue
executing rather than returning as it normally would. By
default
.B exp_continue
resets the timeout timer. The
.I -continue_timer
flag prevents timer from being restarted. (See
.B expect
for more information.)
.TP
.BI exp_internal " [\-f file] value"
causes further commands to send diagnostic information internal to
.B Expect
to stderr if
.I value
is non-zero. This output is disabled if
.I value
is 0. The diagnostic information includes every character received,
and every attempt made to match the current output against the patterns.
.IP
If the optional
.I file
is supplied, all normal and debugging output is written to that file
(regardless of the value of
.IR value ).
Any previous diagnostic output file is closed.
The
.B \-info
flag causes exp_internal to return a description of the
most recent non-info arguments given.
.TP
.BI exp_open " [args] [\-i spawn_id]"
returns a Tcl file identifier that corresponds to the original spawn id.
The file identifier can then be used as if it were opened by Tcl's
.B open
command. (The spawn id should no longer be used. A
.B wait
should not be executed.
The
.B \-leaveopen
flag leaves the spawn id open for access through
Expect commands. A
.B wait
must be executed on the spawn id.
.TP
.BI exp_pid " [\-i spawn_id]"
returns the process id corresponding to the currently spawned process.
If the
.B \-i
flag is used, the pid returned corresponds to that of the given spawn id.
.TP
.B exp_send
is an alias for
.BR send .
.TP
.B exp_send_error
is an alias for
.BR send_error .
.TP
.B exp_send_log
is an alias for
.BR send_log .
.TP
.B exp_send_tty
is an alias for
.BR send_tty .
.TP
.B exp_send_user
is an alias for
.BR send_user .
.TP
.BI exp_version " [[\-exit] version]"
is useful for assuring that the script is compatible with the current
version of Expect.
.IP
With no arguments, the current version of
.B Expect
is returned. This version
may then be encoded in your script. If you actually know that you are not
using features of recent versions, you can specify an earlier version.
.IP
Versions consist of three numbers separated by dots. First
is the major number. Scripts written for versions of
.B Expect
with a
different major number will almost certainly not work.
.B exp_version
returns an error if the major numbers do not match.
.IP
Second is the minor number. Scripts written for a version with a
greater minor number than the current version
may depend upon some new feature and might not run.
.B exp_version
returns an error if the major numbers match, but the script minor number
is greater than that of the running
.BR Expect .
.IP
Third is a number that plays no part in the version comparison.
However, it is incremented when the
.B Expect
software
distribution is changed in any way, such as by additional documentation
or optimization. It is reset to 0 upon each new minor version.
.IP
With the
.B \-exit
flag,
.B Expect
prints an error and exits if the version is out of date.
.TP
.BI expect " [[\-opts] pat1 body1] ... [\-opts] patn [bodyn]"
waits until one of the patterns matches the output of a spawned process,
a specified time period has passed, or an end-of-file is seen.
If the final body is empty, it may be omitted.
.IP
Patterns from the most recent
.B expect_before
command are implicitly used before any other patterns.
Patterns from the most recent
.B expect_after
command are implicitly used after any other patterns.
.IP
If the arguments to the entire
.B expect
statement require more than one line,
all the arguments may be "braced" into one so as to avoid terminating each
line with a backslash. In this one case, the usual Tcl substitutions will
occur despite the braces.
.IP
If a pattern is the keyword
.BR eof ,
the corresponding body is executed upon end-of-file.
If a pattern is the keyword
.BR timeout ,
the corresponding body is executed upon timeout. If no timeout keyword
is used, an implicit null action is executed upon timeout.
The default timeout period is 10 seconds but may be set, for example to 30,
by the command "set timeout 30". An infinite timeout may be designated
by the value \-1.
If a pattern is the keyword
.BR default ,
the corresponding body is executed upon either timeout or end-of-file.
.IP
If a pattern matches, then the corresponding body is executed.
.B expect
returns the result of the body (or the empty string if no pattern matched).
In the event that multiple patterns match, the one appearing first is
used to select a body.
.IP
Each time new output arrives, it is compared to each pattern in the order
they are listed. Thus, you may test for absence of a match by making
the last pattern something guaranteed to appear, such as a prompt.
In situations where there is no prompt, you must use
.B timeout
(just like you would if you were interacting manually).
.IP
Patterns are specified in three ways. By default,
patterns are specified as with Tcl's
.B string match
command. (Such patterns are also similar to C-shell regular expressions
usually referred to as "glob" patterns). The
.B \-gl
flag may may
be used to protect patterns that might otherwise match
.B expect
flags from doing so.
Any pattern beginning with a "-" should be protected this way. (All strings
starting with "-" are reserved for future options.)
.IP
For example, the following fragment looks for a successful login.
(Note that
.B abort
is presumed to be a procedure defined elsewhere in the script.)
.nf
.ta \w' expect 'u +\w'invalid password 'u
expect {
busy {puts busy\\n ; exp_continue}
failed abort
"invalid password" abort
timeout abort
connected
}
.fi
Quotes are necessary on the fourth pattern since it contains a space, which
would otherwise separate the pattern from the action.
Patterns with the same action (such as the 3rd and 4th) require listing the
actions again. This can be avoid by using regexp-style patterns (see below).
More information on forming glob-style patterns can be found in the Tcl manual.
.IP
Regexp-style patterns follow the syntax defined by Tcl's
.B regexp
(short for "regular expression") command.
regexp patterns are introduced with the flag
.BR \-re .
The previous example can be rewritten using a regexp as:
.nf
.ta \w' expect 'u +\w'connected 'u
expect {
busy {puts busy\\n ; exp_continue}
\-re "failed|invalid password" abort
timeout abort
connected
}
.fi
Both types of patterns are "unanchored". This means that patterns
do not have to match the entire string, but can begin and end the
match anywhere in the string (as long as everything else matches).
Use ^ to match the beginning of a string, and $ to match the end.
Note that if you do not wait for the end of a string, your responses
can easily end up in the middle of the string as they are echoed from
the spawned process. While still producing correct results, the output
can look unnatural. Thus, use of $ is encouraged if you can exactly
describe the characters at the end of a string.
Note that in many editors, the ^ and $ match the beginning and end of
lines respectively. However, because expect is not line oriented,
these characters match the beginning and end of the data (as opposed
to lines) currently in the expect matching buffer. (Also, see the
note below on "system indigestion.")
The
.B \-ex
flag causes the pattern to be matched as an "exact" string. No
interpretation of *, ^, etc is made (although the usual Tcl
conventions must still be observed).
Exact patterns are always unanchored.
.IP
The
.B \-nocase
flag causes uppercase characters of the output to compare as if they were
lowercase characters. The pattern is not affected.
.IP
While reading output,
more than 2000 bytes can force earlier bytes to be "forgotten".
This may be changed with the function
.BR match_max .
(Note that excessively large values can slow down the pattern matcher.)
If
.I patlist
is
.BR full_buffer ,
the corresponding body is executed if
.I match_max
bytes have been received and no other patterns have matched.
Whether or not the
.B full_buffer
keyword is used, the forgotten characters are written to
expect_out(buffer).
If
.I patlist
is the keyword
.BR null ,
and nulls are allowed (via the
.B remove_nulls
command), the corresponding body is executed if a single ASCII
0 is matched.
It is not possible to
match 0 bytes via glob or regexp patterns.
Upon matching a pattern (or eof or full_buffer),
any matching and previously unmatched output is saved in the variable
.IR expect_out(buffer) .
Up to 9 regexp substring matches are saved in the variables
.I expect_out(1,string)
through
.IR expect_out(9,string) .
If the
.B -indices
flag is used before a pattern,
the starting and ending indices (in a form suitable for
.BR lrange )
of the
10 strings are stored in the variables
.I expect_out(X,start)
and
.I expect_out(X,end)
where X is a digit, corresponds to the substring position in the buffer.
0 refers to strings which matched the entire pattern
and is generated for glob patterns as well as regexp patterns.
For example, if a process has produced output of "abcdefgh\\n", the result of:
.nf
expect "cd"
.fi
is as if the following statements had executed:
.nf
set expect_out(0,string) cd
set expect_out(buffer) abcd
.fi
and "efgh\\n" is left in the output buffer.
If a process produced the output "abbbcabkkkka\\n", the result of:
.nf
expect \-indices \-re "b(b*).*(k+)"
.fi
is as if the following statements had executed:
.nf
set expect_out(0,start) 1
set expect_out(0,end) 10
set expect_out(0,string) bbbcabkkkk
set expect_out(1,start) 2
set expect_out(1,end) 3
set expect_out(1,string) bb
set expect_out(2,start) 10
set expect_out(2,end) 10
set expect_out(2,string) k
set expect_out(buffer) abbbcabkkkk
.fi
and "a\\n" is left in the output buffer. The pattern "*" (and -re ".*") will
flush the output buffer without reading any more output from the
process.
.IP
Normally, the matched output is discarded from Expect's internal buffers.
This may be prevented by prefixing a pattern with the
.B \-notransfer
flag. This flag is especially useful in experimenting (and can be
abbreviated to "-not" for convenience while experimenting).
The spawn id associated with the matching output (or eof or
full_buffer) is stored in
.IR expect_out(spawn_id) .
The
.B \-timeout
flag causes the current expect command to use the following value
as a timeout instead of using the value of the timeout variable.
By default,
patterns are matched against output from the current process, however the
.B \-i
flag declares the output from the named spawn_id list be matched against
any following patterns (up to the next
.BR \-i ).
The spawn_id list should either be a whitespace separated list of spawn_ids
or a variable referring to such a list of spawn_ids.
For example, the following example waits for
"connected" from the current process, or "busy", "failed" or "invalid
password" from the spawn_id named by $proc2.
.nf
expect {
\-i $proc2 busy {puts busy\\n ; exp_continue}
\-re "failed|invalid password" abort
timeout abort
connected
}
.fi
The value of the global variable
.I any_spawn_id
may be used to match patterns to any spawn_ids that are named
with all other
.B \-i
flags in the current
.B expect
command.
The spawn_id from a
.B \-i
flag with no associated pattern (i.e., followed immediately
by another
.BR \-i )
is made available to any other patterns
in the same
.B expect
command associated with
.I any_spawn_id.
The
.B \-i
flag may also name a global variable in which case the variable is read
for a list of spawn ids. The variable is reread whenever it changes.
This provides a way of changing the I/O source while the command is in
execution. Spawn ids provided this way are called "indirect" spawn ids.
Actions such as
.B break
and
.B continue
cause control structures (i.e.,
.BR for ,
.BR proc )
to behave in the usual way.
The command
.B exp_continue
allows
.B expect
itself to continue
executing rather than returning as it normally would.
.IP
This is useful for avoiding explicit loops or repeated expect statements.
The following example is part of a fragment to automate rlogin. The
.B exp_continue
avoids having to write a second
.B expect
statement (to look for the prompt again) if the rlogin prompts for a password.
.nf
expect {
Password: {
stty -echo
send_user "password (for $user) on $host: "
expect_user -re "(.*)\\n"
send_user "\\n"
send "$expect_out(1,string)\\r"
stty echo
exp_continue
} incorrect {
send_user "invalid password or account\\n"
exit
} timeout {
send_user "connection to $host timed out\\n"
exit
} eof {
send_user \\
"connection to host failed: $expect_out(buffer)"
exit
} -re $prompt
}
.fi
For example, the following fragment might help a user guide
an interaction that is already totally automated. In this case, the terminal
is put into raw mode. If the user presses "+", a variable is incremented.
If "p" is pressed, several returns are sent to the process,
perhaps to poke it in some way, and "i" lets the user interact with the
process, effectively stealing away control from the script.
In each case, the
.B exp_continue
allows the current
.B expect
to continue pattern matching after executing the
current action.
.nf
stty raw \-echo
expect_after {
\-i $user_spawn_id
"p" {send "\\r\\r\\r"; exp_continue}
"+" {incr foo; exp_continue}
"i" {interact; exp_continue}
"quit" exit
}
.fi
.IP
By default,
.B exp_continue
resets the timeout timer. The timer is not restarted, if
.B exp_continue
is called with the
.B \-continue_timer
flag.
.TP
.BI expect_after " [expect_args]"
works identically to the
.B expect_before
except that if patterns from both
.B expect
and
.B expect_after
can match, the
.B expect
pattern is used. See the
.B expect_before
command for more information.
.TP
.BI expect_background " [expect_args]"
takes the same arguments as
.BR expect ,
however it returns immediately.
Patterns are tested whenever new input arrives.
The pattern
.B timeout
and
.B default
are meaningless to
.BR expect_background
and are silently discarded.
Otherwise, the
.B expect_background
command uses
.B expect_before
and
.B expect_after
patterns just like
.B expect
does.
When
.B expect_background
actions are being evaluated, background processing for the same
spawn id is blocked. Background processing is unblocked when
the action completes. While background processing is blocked,
it is possible to do a (foreground)
.B expect
on the same spawn id.
It is not possible to execute an
.B expect
while an
.B expect_background
is unblocked.
.B expect_background
for a particular spawn id is deleted by
declaring a new expect_background with the same spawn id. Declaring
.B expect_background
with no pattern removes the given spawn id
from the ability to match patterns in the background.
.TP
.BI expect_before " [expect_args]"
takes the same arguments as
.BR expect ,
however it returns immediately.
Pattern-action pairs from the most recent
.B expect_before
with the same spawn id are implicitly added to any following
.B expect
commands. If a pattern matches, it is treated as if it had been
specified in the
.B expect
command itself, and the associated body is executed in the context
of the
.B expect