Debian Codegraph @

upstream/fedora-44/man1/expect.1

raw
1 .TH EXPECT 1 "29 December 1994"
2 .SH NAME
3 expect \- programmed dialogue with interactive programs, Version 5
4 .SH SYNOPSIS
5 .B expect
6 [
7 .B \-dDhinNv
8 ]
9 [
10 .B \-c
11 .I cmds
12 ]
13 [
14 [
15 .BR \- [ f | b ]
16 ]
17 .I cmdfile
18 ]
19 [
20 .I args
21 ]
22 .SH INTRODUCTION
23 .B Expect
24 is a program that "talks" to other interactive programs according to a
25 script. Following the script,
26 .B Expect
27 knows what can be expected from
28 a program and what the correct response should be. An interpreted
29 language provides branching and high-level control structures to
30 direct the dialogue. In addition, the user can take control
31 and interact directly when desired, afterward returning control to the
32 script.
33 .PP
34 .B Expectk
35 is a mixture of
36 .B Expect
37 and
38 .BR Tk .
39 It behaves just like
40 .B Expect
41 and
42 .BR Tk 's
43 .BR wish .
44 .B Expect
45 can also be used directly in C or C++ (that is, without Tcl).
46 See libexpect(3).
47 .PP
48 The name "Expect" comes from the idea of
49 .I send/expect
50 sequences popularized
51 by uucp, kermit and other modem control programs.
52 However unlike uucp,
53 .B Expect
54 is generalized so that it can be run as a user-level command
55 with any program and task in mind.
56 .B Expect
57 can actually talk to several programs at the same time.
58 .PP
59 For example, here are some things
60 .B Expect
61 can do:
62 .RS
63 .TP 4
64 \(bu
65 Cause your computer to dial you back,
66 so that you can login without paying for the call.
67 .TP
68 \(bu
69 Start a game (e.g., rogue) and if the optimal configuration doesn't appear,
70 restart it (again and again) until it does,
71 then hand over control to you.
72 .TP
73 \(bu
74 Run fsck, and in response to its questions, answer "yes", "no" or give control back to you,
75 based on predetermined criteria.
76 .TP
77 \(bu
78 Connect to another network or BBS (e.g., MCI Mail, CompuServe) and
79 automatically retrieve your mail so that it appears as if
80 it was originally sent to your local system.
81 .TP
82 \(bu
83 Carry environment variables, current directory,
84 or any kind of information across rlogin, telnet, tip, su, chgrp, etc.
85 .RE
86 .PP
87 There are a variety of reasons why the shell cannot perform these tasks.
88 (Try, you'll see.)
89 All are possible with
90 .BR Expect .
91 .PP
92 In general,
93 .B Expect
94 is useful for running any program which requires
95 interaction between the program and the user.
96 All that is necessary is that the interaction can be characterized
97 programmatically.
98 .B Expect
99 can also give the user back control
100 (without halting the program being controlled) if desired.
101 Similarly, the user can return control to the script at any time.
102 .SH USAGE
103 .B Expect
104 reads
105 .I cmdfile
106 for a list of commands to execute.
107 .B Expect
108 may also be invoked implicitly on systems which support the #! notation
109 by marking the script executable, and making the first line in your script:
110
111 #!/usr/local/bin/expect \-f
112
113 Of course, the path must accurately describe where
114 .B Expect
115 lives. /usr/local/bin is just an example.
116
117 The
118 .B \-c
119 flag prefaces a command to be executed before any in the script.
120 The command should be quoted to prevent being broken up by the shell.
121 This option may be used multiple times.
122 Multiple commands may be
123 executed with a single
124 .B \-c
125 by separating them with semicolons.
126 Commands are executed in the order they appear.
127 (When using Expectk, this option is specified as
128 .BR \-command .)
129 .PP
130 The
131 .B \-d
132 flag enables some diagnostic output, which
133 primarily reports internal activity of commands such as
134 .B expect
135 and
136 .BR interact .
137 This flag has the same effect as "exp_internal 1" at the beginning of an Expect
138 script, plus the version of
139 .B Expect
140 is printed.
141 (The
142 .B strace
143 command is useful for tracing statements, and the
144 .B trace
145 command is useful for tracing variable assignments.)
146 (When using Expectk, this option is specified as
147 .BR \-diag .)
148 .PP
149 The
150 .B \-D
151 flag enables an interactive debugger. An integer value should follow.
152 The debugger will take control before the next Tcl procedure
153 if the value is non-zero
154 or if a ^C is pressed (or a breakpoint is hit, or other appropriate debugger
155 command appears in the script). See the README file or SEE ALSO (below)
156 for more information on the debugger.
157 (When using Expectk, this option is specified as
158 .BR \-Debug .)
159 .PP
160 The
161 .B \-f
162 flag prefaces a file from which to read commands from.
163 The flag itself is optional as it is only useful when using
164 the #! notation (see above),
165 so that other arguments may be supplied on the command line.
166 (When using Expectk, this option is specified as
167 .BR \-file .)
168 .PP
169 By default, the command file is read into memory and executed in its entirety.
170 It is occasionally desirable to read files one line at a time. For example,
171 stdin is read this way. In order to force arbitrary files to be handled this
172 way, use the
173 .B \-b
174 flag.
175 (When using Expectk, this option is specified as
176 .BR \-buffer .)
177 Note that stdio-buffering may still take place however this shouldn't cause
178 problems when reading from a fifo or stdin.
179 .PP
180 If the string "\-" is supplied as a filename, standard input is read instead.
181 (Use "./\-" to read from a file actually named "\-".)
182 .PP
183 .B \-h
184 causes Expect to print its usage message and exit.
185 .PP
186 The
187 .B \-i
188 flag causes
189 .B Expect
190 to interactively prompt for commands instead of reading
191 them from a file.
192 Prompting is terminated via the
193 .B exit
194 command or upon EOF.
195 See
196 .B interpreter
197 (below) for more information.
198 .B \-i
199 is assumed if neither a command file nor
200 .B \-c
201 is used.
202 (When using Expectk, this option is specified as
203 .BR \-interactive .)
204 .PP
205 .B \-\-
206 may be used to delimit the end of the options. This is useful if
207 you want to pass an option-like argument to your script without it being
208 interpreted by
209 .BR Expect .
210 This can usefully be placed in the #! line to prevent any flag-like
211 interpretation by Expect. For example, the following will leave the
212 original arguments (including the script name) in the variable
213 .IR argv .
214
215 #!/usr/local/bin/expect \-\-
216
217 Note that the usual getopt(3) and execve(2) conventions must be observed
218 when adding arguments to the #! line.
219 .PP
220 The file $exp_library/expect.rc is sourced automatically if present, unless
221 the
222 .B \-N
223 flag is used.
224 (When using Expectk, this option is specified as
225 .BR \-NORC .)
226 Immediately after this,
227 the file ~/.expect.rc is sourced automatically, unless the
228 .B \-n
229 flag is used. If the environment variable DOTDIR is defined,
230 it is treated as a directory and .expect.rc is read from there.
231 (When using Expectk, this option is specified as
232 .BR \-norc .)
233 This sourcing occurs only after executing any
234 .B \-c
235 flags.
236 .PP
237 .B \-v
238 causes Expect to print its version number and exit. (The corresponding flag
239 in Expectk, which uses long flag names, is \-version.)
240 .PP
241 Optional
242 .I args
243 are constructed into a list and stored in the variable named
244 .IR argv .
245 .I argc
246 is initialized to the length of argv.
247 .PP
248 .I argv0
249 is defined to be the name of the script (or binary if no script is used).
250 For example,
251 the following prints out the name of the script and the first three arguments:
252 .nf
253
254 send_user "$argv0 [lrange $argv 0 2]\\n"
255
256 .fi
257 .SH COMMANDS
258 .B Expect
259 uses
260 .I Tcl
261 (Tool Command Language).
262 Tcl provides control flow (e.g., if, for, break),
263 expression evaluation and several other features such as recursion,
264 procedure definition, etc.
265 Commands used here but not defined (e.g.,
266 .BR set ,
267 .BR if ,
268 .BR exec )
269 are Tcl commands (see tcl(3)).
270 .B Expect
271 supports additional commands, described below.
272 Unless otherwise specified, commands return the empty string.
273 .PP
274 Commands are listed alphabetically so that they can be quickly located.
275 However, new users may find it easier to start by reading the descriptions
276 of
277 .BR spawn ,
278 .BR send ,
279 .BR expect ,
280 and
281 .BR interact ,
282 in that order.
283
284 Note that the best introduction to the language (both Expect and Tcl)
285 is provided in the book "Exploring Expect" (see SEE ALSO below).
286 Examples are included in this man page but they are very limited since
287 this man page is meant primarily as reference material.
288
289 Note that in the text of this man page, "Expect" with an uppercase "E"
290 refers to the
291 .B Expect
292 program while "expect" with a lower-case "e" refers to the
293 .B expect
294 command within the
295 .B Expect
296 program.)
297 .I
298 .TP 6
299 .BI close " [-slave] [\-onexec 0|1] [\-i spawn_id]"
300 closes the connection to the current process.
301 Most interactive programs will detect EOF on their stdin and exit;
302 thus
303 .B close
304 usually suffices to kill the process as well.
305 The
306 .B \-i
307 flag declares the process to close corresponding to the named spawn_id.
308
309 Both
310 .B expect
311 and
312 .B interact
313 will detect when the current process exits and implicitly do a
314 .BR close .
315 But if you kill the process by, say, "exec kill $pid",
316 you will need to explicitly call
317 .BR close .
318
319 The
320 .BR \-onexec
321 flag determines whether the spawn id will be closed in any new spawned
322 processes or if the process is overlayed. To leave a spawn id open,
323 use the value 0. A non-zero integer value will force the spawn closed
324 (the default) in any new processes.
325
326 The
327 .B \-slave
328 flag closes the slave associated with the spawn id. (See "spawn -pty".)
329 When the connection is closed, the slave is automatically closed as
330 well if still open.
331
332 No matter whether the connection is closed implicitly or explicitly,
333 you should call
334 .B wait
335 to clear up the corresponding kernel process slot.
336 .B close
337 does not call
338 .B wait
339 since there is no guarantee that closing a process connection will cause
340 it to exit.
341 See
342 .B wait
343 below for more info.
344 .TP
345 .BI debug " [[-now] 0|1]"
346 controls a Tcl debugger allowing you to step through statements, set
347 breakpoints, etc.
348
349 With no arguments, a 1 is returned if the debugger is not running, otherwise
350 a 0 is returned.
351
352 With a 1 argument, the debugger is started. With a 0 argument, the
353 debugger is stopped. If a 1 argument is preceded by the
354 .B \-now
355 flag, the debugger is started immediately (i.e., in the middle of the
356 .B debug
357 command itself). Otherwise, the debugger is started with the next
358 Tcl statement.
359
360 The
361 .B debug
362 command does not change any traps. Compare this to starting Expect with the
363 .B -D
364 flag (see above).
365
366 See the README file or SEE ALSO (below)
367 for more information on the debugger.
368 .TP
369 .B disconnect
370 disconnects a forked process from the terminal. It continues running in the
371 background. The process is given its own process group (if possible).
372 Standard I/O is redirected to /dev/null.
373 .IP
374 The following fragment uses
375 .B disconnect
376 to continue running the script in the background.
377 .nf
378
379 if {[fork]!=0} exit
380 disconnect
381 . . .
382
383 .fi
384 The following script reads a password, and then runs a program
385 every hour that demands a password each time it is run. The script supplies
386 the password so that you only have to type it once.
387 (See the
388 .B stty
389 command which demonstrates how to turn off password echoing.)
390 .nf
391
392 send_user "password?\\ "
393 expect_user -re "(.*)\\n"
394 for {} 1 {} {
395 if {[fork]!=0} {sleep 3600;continue}
396 disconnect
397 spawn priv_prog
398 expect Password:
399 send "$expect_out(1,string)\\r"
400 . . .
401 exit
402 }
403
404 .fi
405 An advantage to using
406 .B disconnect
407 over the shell asynchronous process feature (&) is that
408 .B Expect
409 can
410 save the terminal parameters prior to disconnection, and then later
411 apply them to new ptys. With &,
412 .B Expect
413 does not have a chance
414 to read the terminal's parameters since the terminal is already
415 disconnected by the time
416 .B Expect
417 receives control.
418 .TP
419 .BI exit " [\-opts] [status]"
420 causes
421 .B Expect
422 to exit or otherwise prepare to do so.
423
424 The
425 .B \-onexit
426 flag causes the next argument to be used as an exit handler.
427 Without an argument, the current exit handler is returned.
428
429 The
430 .B \-noexit
431 flag causes
432 .B Expect
433 to prepare to exit but stop short of actually returning control to the
434 operating system. The user-defined exit handler is run as well as Expect's
435 own internal handlers.
436 No further Expect commands should be executed.
437 This is useful if you are running Expect with other Tcl extensions.
438 The current interpreter (and main window if in the Tk environment) remain
439 so that other Tcl extensions can clean up. If Expect's
440 .B exit
441 is called again (however this might occur), the handlers are not rerun.
442
443 Upon exiting,
444 all connections to spawned processes are closed. Closure will be detected
445 as an EOF by spawned processes.
446 .B exit
447 takes no other actions beyond what the normal _exit(2) procedure does.
448 Thus, spawned processes that do not check for EOF may continue to run.
449 (A variety of conditions are important to determining, for example, what
450 signals a spawned process will be sent, but these are system-dependent,
451 typically documented under exit(3).)
452 Spawned processes that continue to run will be inherited by init.
453
454 .I status
455 (or 0 if not specified) is returned as the exit status of
456 .BR Expect .
457 .B exit
458 is implicitly executed if the end of the script is reached.
459 .TP
460 \fBexp_continue\fR [-continue_timer]
461 The command
462 .B exp_continue
463 allows
464 .B expect
465 itself to continue
466 executing rather than returning as it normally would. By
467 default
468 .B exp_continue
469 resets the timeout timer. The
470 .I -continue_timer
471 flag prevents timer from being restarted. (See
472 .B expect
473 for more information.)
474 .TP
475 .BI exp_internal " [\-f file] value"
476 causes further commands to send diagnostic information internal to
477 .B Expect
478 to stderr if
479 .I value
480 is non-zero. This output is disabled if
481 .I value
482 is 0. The diagnostic information includes every character received,
483 and every attempt made to match the current output against the patterns.
484 .IP
485 If the optional
486 .I file
487 is supplied, all normal and debugging output is written to that file
488 (regardless of the value of
489 .IR value ).
490 Any previous diagnostic output file is closed.
491
492 The
493 .B \-info
494 flag causes exp_internal to return a description of the
495 most recent non-info arguments given.
496 .TP
497 .BI exp_open " [args] [\-i spawn_id]"
498 returns a Tcl file identifier that corresponds to the original spawn id.
499 The file identifier can then be used as if it were opened by Tcl's
500 .B open
501 command. (The spawn id should no longer be used. A
502 .B wait
503 should not be executed.
504
505 The
506 .B \-leaveopen
507 flag leaves the spawn id open for access through
508 Expect commands. A
509 .B wait
510 must be executed on the spawn id.
511 .TP
512 .BI exp_pid " [\-i spawn_id]"
513 returns the process id corresponding to the currently spawned process.
514 If the
515 .B \-i
516 flag is used, the pid returned corresponds to that of the given spawn id.
517 .TP
518 .B exp_send
519 is an alias for
520 .BR send .
521 .TP
522 .B exp_send_error
523 is an alias for
524 .BR send_error .
525 .TP
526 .B exp_send_log
527 is an alias for
528 .BR send_log .
529 .TP
530 .B exp_send_tty
531 is an alias for
532 .BR send_tty .
533 .TP
534 .B exp_send_user
535 is an alias for
536 .BR send_user .
537 .TP
538 .BI exp_version " [[\-exit] version]"
539 is useful for assuring that the script is compatible with the current
540 version of Expect.
541 .IP
542 With no arguments, the current version of
543 .B Expect
544 is returned. This version
545 may then be encoded in your script. If you actually know that you are not
546 using features of recent versions, you can specify an earlier version.
547 .IP
548 Versions consist of three numbers separated by dots. First
549 is the major number. Scripts written for versions of
550 .B Expect
551 with a
552 different major number will almost certainly not work.
553 .B exp_version
554 returns an error if the major numbers do not match.
555 .IP
556 Second is the minor number. Scripts written for a version with a
557 greater minor number than the current version
558 may depend upon some new feature and might not run.
559 .B exp_version
560 returns an error if the major numbers match, but the script minor number
561 is greater than that of the running
562 .BR Expect .
563 .IP
564 Third is a number that plays no part in the version comparison.
565 However, it is incremented when the
566 .B Expect
567 software
568 distribution is changed in any way, such as by additional documentation
569 or optimization. It is reset to 0 upon each new minor version.
570 .IP
571 With the
572 .B \-exit
573 flag,
574 .B Expect
575 prints an error and exits if the version is out of date.
576 .TP
577 .BI expect " [[\-opts] pat1 body1] ... [\-opts] patn [bodyn]"
578 waits until one of the patterns matches the output of a spawned process,
579 a specified time period has passed, or an end-of-file is seen.
580 If the final body is empty, it may be omitted.
581 .IP
582 Patterns from the most recent
583 .B expect_before
584 command are implicitly used before any other patterns.
585 Patterns from the most recent
586 .B expect_after
587 command are implicitly used after any other patterns.
588 .IP
589 If the arguments to the entire
590 .B expect
591 statement require more than one line,
592 all the arguments may be "braced" into one so as to avoid terminating each
593 line with a backslash. In this one case, the usual Tcl substitutions will
594 occur despite the braces.
595 .IP
596 If a pattern is the keyword
597 .BR eof ,
598 the corresponding body is executed upon end-of-file.
599 If a pattern is the keyword
600 .BR timeout ,
601 the corresponding body is executed upon timeout. If no timeout keyword
602 is used, an implicit null action is executed upon timeout.
603 The default timeout period is 10 seconds but may be set, for example to 30,
604 by the command "set timeout 30". An infinite timeout may be designated
605 by the value \-1.
606 If a pattern is the keyword
607 .BR default ,
608 the corresponding body is executed upon either timeout or end-of-file.
609 .IP
610 If a pattern matches, then the corresponding body is executed.
611 .B expect
612 returns the result of the body (or the empty string if no pattern matched).
613 In the event that multiple patterns match, the one appearing first is
614 used to select a body.
615 .IP
616 Each time new output arrives, it is compared to each pattern in the order
617 they are listed. Thus, you may test for absence of a match by making
618 the last pattern something guaranteed to appear, such as a prompt.
619 In situations where there is no prompt, you must use
620 .B timeout
621 (just like you would if you were interacting manually).
622 .IP
623 Patterns are specified in three ways. By default,
624 patterns are specified as with Tcl's
625 .B string match
626 command. (Such patterns are also similar to C-shell regular expressions
627 usually referred to as "glob" patterns). The
628 .B \-gl
629 flag may
630 be used to protect patterns that might otherwise match
631 .B expect
632 flags from doing so.
633 Any pattern beginning with a "-" should be protected this way. (All strings
634 starting with "-" are reserved for future options.)
635
636 .IP
637 For example, the following fragment looks for a successful login.
638 (Note that
639 .B abort
640 is presumed to be a procedure defined elsewhere in the script.)
641 .nf
642
643 .ta \w' expect 'u +\w'invalid password 'u
644 expect {
645 busy {puts busy\\n ; exp_continue}
646 failed abort
647 "invalid password" abort
648 timeout abort
649 connected
650 }
651
652 .fi
653 Quotes are necessary on the fourth pattern since it contains a space, which
654 would otherwise separate the pattern from the action.
655 Patterns with the same action (such as the 3rd and 4th) require listing the
656 actions again. This can be avoid by using regexp-style patterns (see below).
657 More information on forming glob-style patterns can be found in the Tcl manual.
658 .IP
659 Regexp-style patterns follow the syntax defined by Tcl's
660 .B regexp
661 (short for "regular expression") command.
662 regexp patterns are introduced with the flag
663 .BR \-re .
664 The previous example can be rewritten using a regexp as:
665 .nf
666
667 .ta \w' expect 'u +\w'connected 'u
668 expect {
669 busy {puts busy\\n ; exp_continue}
670 \-re "failed|invalid password" abort
671 timeout abort
672 connected
673 }
674
675 .fi
676 Both types of patterns are "unanchored". This means that patterns
677 do not have to match the entire string, but can begin and end the
678 match anywhere in the string (as long as everything else matches).
679 Use ^ to match the beginning of a string, and $ to match the end.
680 Note that if you do not wait for the end of a string, your responses
681 can easily end up in the middle of the string as they are echoed from
682 the spawned process. While still producing correct results, the output
683 can look unnatural. Thus, use of $ is encouraged if you can exactly
684 describe the characters at the end of a string.
685
686 Note that in many editors, the ^ and $ match the beginning and end of
687 lines respectively. However, because expect is not line oriented,
688 these characters match the beginning and end of the data (as opposed
689 to lines) currently in the expect matching buffer. (Also, see the
690 note below on "system indigestion.")
691
692 The
693 .B \-ex
694 flag causes the pattern to be matched as an "exact" string. No
695 interpretation of *, ^, etc is made (although the usual Tcl
696 conventions must still be observed).
697 Exact patterns are always unanchored.
698
699 .IP
700 The
701 .B \-nocase
702 flag causes uppercase characters of the output to compare as if they were
703 lowercase characters. The pattern is not affected.
704 .IP
705 While reading output,
706 more than 2000 bytes can force earlier bytes to be "forgotten".
707 This may be changed with the function
708 .BR match_max .
709 (Note that excessively large values can slow down the pattern matcher.)
710 If
711 .I patlist
712 is
713 .BR full_buffer ,
714 the corresponding body is executed if
715 .I match_max
716 bytes have been received and no other patterns have matched.
717 Whether or not the
718 .B full_buffer
719 keyword is used, the forgotten characters are written to
720 expect_out(buffer).
721
722 If
723 .I patlist
724 is the keyword
725 .BR null ,
726 and nulls are allowed (via the
727 .B remove_nulls
728 command), the corresponding body is executed if a single ASCII
729 0 is matched.
730 It is not possible to
731 match 0 bytes via glob or regexp patterns.
732
733 Upon matching a pattern (or eof or full_buffer),
734 any matching and previously unmatched output is saved in the variable
735 .IR expect_out(buffer) .
736 Up to 9 regexp substring matches are saved in the variables
737 .I expect_out(1,string)
738 through
739 .IR expect_out(9,string) .
740 If the
741 .B -indices
742 flag is used before a pattern,
743 the starting and ending indices (in a form suitable for
744 .BR lrange )
745 of the
746 10 strings are stored in the variables
747 .I expect_out(X,start)
748 and
749 .I expect_out(X,end)
750 where X is a digit, corresponds to the substring position in the buffer.
751 0 refers to strings which matched the entire pattern
752 and is generated for glob patterns as well as regexp patterns.
753 For example, if a process has produced output of "abcdefgh\\n", the result of:
754 .nf
755
756 expect "cd"
757
758 .fi
759 is as if the following statements had executed:
760 .nf
761
762 set expect_out(0,string) cd
763 set expect_out(buffer) abcd
764
765 .fi
766 and "efgh\\n" is left in the output buffer.
767 If a process produced the output "abbbcabkkkka\\n", the result of:
768 .nf
769
770 expect \-indices \-re "b(b*).*(k+)"
771
772 .fi
773 is as if the following statements had executed:
774 .nf
775
776 set expect_out(0,start) 1
777 set expect_out(0,end) 10
778 set expect_out(0,string) bbbcabkkkk
779 set expect_out(1,start) 2
780 set expect_out(1,end) 3
781 set expect_out(1,string) bb
782 set expect_out(2,start) 10
783 set expect_out(2,end) 10
784 set expect_out(2,string) k
785 set expect_out(buffer) abbbcabkkkk
786
787 .fi
788 and "a\\n" is left in the output buffer. The pattern "*" (and -re ".*") will
789 flush the output buffer without reading any more output from the
790 process.
791 .IP
792 Normally, the matched output is discarded from Expect's internal buffers.
793 This may be prevented by prefixing a pattern with the
794 .B \-notransfer
795 flag. This flag is especially useful in experimenting (and can be
796 abbreviated to "-not" for convenience while experimenting).
797
798 The spawn id associated with the matching output (or eof or
799 full_buffer) is stored in
800 .IR expect_out(spawn_id) .
801
802 The
803 .B \-timeout
804 flag causes the current expect command to use the following value
805 as a timeout instead of using the value of the timeout variable.
806
807 By default,
808 patterns are matched against output from the current process, however the
809 .B \-i
810 flag declares the output from the named spawn_id list be matched against
811 any following patterns (up to the next
812 .BR \-i ).
813 The spawn_id list should either be a whitespace separated list of spawn_ids
814 or a variable referring to such a list of spawn_ids.
815
816 For example, the following example waits for
817 "connected" from the current process, or "busy", "failed" or "invalid
818 password" from the spawn_id named by $proc2.
819 .nf
820
821 expect {
822 \-i $proc2 busy {puts busy\\n ; exp_continue}
823 \-re "failed|invalid password" abort
824 timeout abort
825 connected
826 }
827
828 .fi
829 The value of the global variable
830 .I any_spawn_id
831 may be used to match patterns to any spawn_ids that are named
832 with all other
833 .B \-i
834 flags in the current
835 .B expect
836 command.
837 The spawn_id from a
838 .B \-i
839 flag with no associated pattern (i.e., followed immediately
840 by another
841 .BR \-i )
842 is made available to any other patterns
843 in the same
844 .B expect
845 command associated with
846 .I any_spawn_id.
847
848 The
849 .B \-i
850 flag may also name a global variable in which case the variable is read
851 for a list of spawn ids. The variable is reread whenever it changes.
852 This provides a way of changing the I/O source while the command is in
853 execution. Spawn ids provided this way are called "indirect" spawn ids.
854
855 Actions such as
856 .B break
857 and
858 .B continue
859 cause control structures (i.e.,
860 .BR for ,
861 .BR proc )
862 to behave in the usual way.
863 The command
864 .B exp_continue
865 allows
866 .B expect
867 itself to continue
868 executing rather than returning as it normally would.
869 .IP
870 This is useful for avoiding explicit loops or repeated expect statements.
871 The following example is part of a fragment to automate rlogin. The
872 .B exp_continue
873 avoids having to write a second
874 .B expect
875 statement (to look for the prompt again) if the rlogin prompts for a password.
876 .nf
877
878 expect {
879 Password: {
880 stty -echo
881 send_user "password (for $user) on $host: "
882 expect_user -re "(.*)\\n"
883 send_user "\\n"
884 send "$expect_out(1,string)\\r"
885 stty echo
886 exp_continue
887 } incorrect {
888 send_user "invalid password or account\\n"
889 exit
890 } timeout {
891 send_user "connection to $host timed out\\n"
892 exit
893 } eof {
894 send_user \\
895 "connection to host failed: $expect_out(buffer)"
896 exit
897 } -re $prompt
898 }
899
900 .fi
901 For example, the following fragment might help a user guide
902 an interaction that is already totally automated. In this case, the terminal
903 is put into raw mode. If the user presses "+", a variable is incremented.
904 If "p" is pressed, several returns are sent to the process,
905 perhaps to poke it in some way, and "i" lets the user interact with the
906 process, effectively stealing away control from the script.
907 In each case, the
908 .B exp_continue
909 allows the current
910 .B expect
911 to continue pattern matching after executing the
912 current action.
913 .nf
914
915 stty raw \-echo
916 expect_after {
917 \-i $user_spawn_id
918 "p" {send "\\r\\r\\r"; exp_continue}
919 "+" {incr foo; exp_continue}
920 "i" {interact; exp_continue}
921 "quit" exit
922 }
923
924 .fi
925 .IP
926 By default,
927 .B exp_continue
928 resets the timeout timer. The timer is not restarted, if
929 .B exp_continue
930 is called with the
931 .B \-continue_timer
932 flag.
933 .TP
934 .BI expect_after " [expect_args]"
935 works identically to the
936 .B expect_before
937 except that if patterns from both
938 .B expect
939 and
940 .B expect_after
941 can match, the
942 .B expect
943 pattern is used. See the
944 .B expect_before
945 command for more information.
946 .TP
947 .BI expect_background " [expect_args]"
948 takes the same arguments as
949 .BR expect ,
950 however it returns immediately.
951 Patterns are tested whenever new input arrives.
952 The pattern
953 .B timeout
954 and
955 .B default
956 are meaningless to
957 .BR expect_background
958 and are silently discarded.
959 Otherwise, the
960 .B expect_background
961 command uses
962 .B expect_before
963 and
964 .B expect_after
965 patterns just like
966 .B expect
967 does.
968
969 When
970 .B expect_background
971 actions are being evaluated, background processing for the same
972 spawn id is blocked. Background processing is unblocked when
973 the action completes. While background processing is blocked,
974 it is possible to do a (foreground)
975 .B expect
976 on the same spawn id.
977
978 It is not possible to execute an
979 .B expect
980 while an
981 .B expect_background
982 is unblocked.
983 .B expect_background
984 for a particular spawn id is deleted by
985 declaring a new expect_background with the same spawn id. Declaring
986 .B expect_background
987 with no pattern removes the given spawn id
988 from the ability to match patterns in the background.
989 .TP
990 .BI expect_before " [expect_args]"
991 takes the same arguments as
992 .BR expect ,
993 however it returns immediately.
994 Pattern-action pairs from the most recent
995 .B expect_before
996 with the same spawn id are implicitly added to any following
997 .B expect
998 commands. If a pattern matches, it is treated as if it had been
999 specified in the
1000 .B expect
1001 command itself, and the associated body is executed in the context
1002 of the
1003 .B expect
1004 command.
1005 If patterns from both
1006 .B expect_before
1007 and
1008 .B expect
1009 can match, the
1010 .B expect_before
1011 pattern is used.
1012
1013 If no pattern is specified, the spawn id is not checked for any patterns.
1014
1015 Unless overridden by a
1016 .B \-i
1017 flag,
1018 .B expect_before
1019 patterns match against the spawn id defined at the time that the
1020 .B expect_before
1021 command was executed (not when its pattern is matched).
1022
1023 The \-info flag causes
1024 .B expect_before
1025 to return the current specifications of what patterns it will match.
1026 By default, it reports on the current spawn id. An optional spawn id specification may be given for information on that spawn id. For example
1027 .nf
1028
1029 expect_before -info -i $proc
1030
1031 .fi
1032 At most one spawn id specification may be given. The flag \-indirect
1033 suppresses direct spawn ids that come only from indirect specifications.
1034
1035 Instead of a spawn id specification, the flag "-all" will cause
1036 "-info" to report on all spawn ids.
1037
1038 The output of the \-info flag can be reused as the argument to expect_before.
1039 .TP
1040 .BI expect_tty " [expect_args]"
1041 is like
1042 .B expect
1043 but it reads characters from /dev/tty (i.e. keystrokes from the user).
1044 By default, reading is performed in cooked mode.
1045 Thus, lines must end with a return in order for
1046 .B expect
1047 to see them.
1048 This may be changed via
1049 .B stty
1050 (see the
1051 .B stty
1052 command below).
1053 .TP
1054 .BI expect_user " [expect_args]"
1055 is like
1056 .B expect
1057 but it reads characters from stdin (i.e. keystrokes from the user).
1058 By default, reading is performed in cooked mode.
1059 Thus, lines must end with a return in order for
1060 .B expect
1061 to see them.
1062 This may be changed via
1063 .B stty
1064 (see the
1065 .B stty
1066 command below).
1067 .TP
1068 .B fork
1069 creates a new process. The new process is an exact copy of the current
1070 .B Expect
1071 process. On success,
1072 .B fork
1073 returns 0 to the new (child) process and returns the process ID of the child
1074 process to the parent process.
1075 On failure (invariably due to lack of resources, e.g., swap space, memory),
1076 .B fork
1077 returns \-1 to the parent process, and no child process is created.
1078 .IP
1079 Forked processes exit via the
1080 .B exit
1081 command, just like the original process.
1082 Forked processes are allowed to write to the log files. If you do not
1083 disable debugging or logging in most of the processes, the result can be
1084 confusing.
1085 .IP
1086 Some pty implementations may be confused by multiple readers and writers,
1087 even momentarily. Thus, it is safest to
1088 .B fork
1089 before spawning processes.
1090 .TP
1091 .BI interact " [string1 body1] ... [stringn [bodyn]]"
1092 gives control of the current process to the user, so that
1093 keystrokes are sent to the current process,
1094 and the stdout and stderr of the current process are returned.
1095 .IP
1096 String-body pairs may be specified as arguments, in which case the
1097 body is executed when the corresponding string is entered. (By default, the
1098 string is not sent to the current process.) The
1099 .B interpreter
1100 command is assumed, if the final body is missing.
1101 .IP
1102 If the arguments to the entire
1103 .B interact
1104 statement require more than one line,
1105 all the arguments may be "braced" into one so as to avoid terminating each
1106 line with a backslash. In this one case, the usual Tcl substitutions will
1107 occur despite the braces.
1108 .IP
1109 For example, the following command runs interact with the following
1110 string-body pairs defined: When ^Z is pressed,
1111 .B Expect
1112 is suspended.
1113 (The
1114 .B \-reset
1115 flag restores the terminal modes.)
1116 When ^A is pressed, the user sees "you typed a control-A" and the
1117 process is sent a ^A. When $ is pressed, the user sees the date.
1118 When ^C is pressed,
1119 .B Expect
1120 exits. If "foo" is entered, the user sees "bar".
1121 When ~~ is pressed, the
1122 .B Expect
1123 interpreter runs interactively.
1124 .nf
1125
1126 .ta \w' interact 'u +\w'$CTRLZ 'u +\w'{'u
1127 set CTRLZ \\032
1128 interact {
1129 -reset $CTRLZ {exec kill \-STOP [pid]}
1130 \\001 {send_user "you typed a control\-A\\n";
1131 send "\\001"
1132 }
1133 $ {send_user "The date is [clock format [clock seconds]]."}
1134 \\003 exit
1135 foo {send_user "bar"}
1136 ~~
1137 }
1138
1139 .fi
1140 .IP
1141 In string-body pairs, strings are matched in the order they are listed
1142 as arguments. Strings that partially match are not sent to the
1143 current process in anticipation of the remainder coming. If
1144 characters are then entered such that there can no longer possibly be
1145 a match, only the part of the string will be sent to the process that cannot
1146 possibly begin another match. Thus, strings that are substrings of
1147 partial matches can match later, if the original strings that was attempting
1148 to be match ultimately fails.
1149 .IP
1150 By default, string matching is exact with no wild cards. (In contrast,
1151 the
1152 .B expect
1153 command uses glob-style patterns by default.) The
1154 .B \-ex
1155 flag may be used to protect patterns that might otherwise match
1156 .B interact
1157 flags from doing so.
1158 Any pattern beginning with a "-" should be protected this way. (All strings
1159 starting with "-" are reserved for future options.)
1160
1161 The
1162 .B \-re
1163 flag forces the string to be interpreted as a regexp-style pattern. In this
1164 case, matching substrings are stored in the variable
1165 .I interact_out
1166 similarly to the way
1167 .B expect
1168 stores its output in the variable
1169 .BR expect_out .
1170 The
1171 .B \-indices
1172 flag is similarly supported.
1173
1174 The pattern
1175 .B eof
1176 introduces an action that is
1177 executed upon end-of-file. A separate
1178 .B eof
1179 pattern may also follow the
1180 .B \-output
1181 flag in which case it is matched if an eof is detected while writing output.
1182 The default
1183 .B eof
1184 action is "return", so that
1185 .B interact
1186 simply returns upon any EOF.
1187
1188 The pattern
1189 .B timeout
1190 introduces a timeout (in seconds) and action that is executed
1191 after no characters have been read for a given time.
1192 The
1193 .B timeout
1194 pattern applies to the most recently specified process.
1195 There is no default timeout.
1196 The special variable "timeout" (used by the
1197 .B expect
1198 command) has no affect on this timeout.
1199
1200 For example, the following statement could be used to autologout users who have
1201 not typed anything for an hour but who still get frequent system
1202 messages:
1203 .nf
1204
1205 interact -input $user_spawn_id timeout 3600 return -output \\
1206 $spawn_id
1207
1208 .fi
1209
1210 If the pattern is the keyword
1211 .BR null ,
1212 and nulls are allowed (via the
1213 .B remove_nulls
1214 command), the corresponding body is executed if a single ASCII
1215 0 is matched.
1216 It is not possible to
1217 match 0 bytes via glob or regexp patterns.
1218
1219 Prefacing a pattern with the flag
1220 .B \-iwrite
1221 causes the variable
1222 .I interact_out(spawn_id)
1223 to be set to the spawn_id which matched the pattern
1224 (or eof).
1225
1226 Actions such as
1227 .B break
1228 and
1229 .B continue
1230 cause control structures (i.e.,
1231 .BR for ,
1232 .BR proc )
1233 to behave in the usual way.
1234 However
1235 .B return
1236 causes interact to return to its caller, while
1237 .B inter_return
1238 causes
1239 .B interact
1240 to cause a return in its caller. For example, if "proc foo" called
1241 .B interact
1242 which then executed the action
1243 .BR inter_return ,
1244 .B proc foo
1245 would return. (This means that if
1246 .B interact
1247 calls
1248 .B interpreter
1249 interactively typing
1250 .B return
1251 will cause the interact to continue, while
1252 .B inter_return
1253 will cause the interact to return to its caller.)
1254 .IP
1255 During
1256 .BR interact ,
1257 raw mode is used so that all characters may be passed to the current process.
1258 If the current process does not catch job control signals,
1259 it will stop if sent a stop signal (by default ^Z).
1260 To restart it, send a continue signal (such as by "kill \-CONT <pid>").
1261 If you really want to send a SIGSTOP to such a process (by ^Z),
1262 consider spawning csh first and then running your program.
1263 On the other hand, if you want to send a SIGSTOP to
1264 .B Expect
1265 itself, first call interpreter (perhaps by using an escape character), and then press ^Z.
1266 .IP
1267 String-body pairs can be used as a shorthand for avoiding having
1268 to enter the interpreter and execute commands interactively. The previous
1269 terminal mode is used while the body of a string-body pair is being executed.
1270 .IP
1271 For speed, actions execute in raw mode by default. The
1272 .B \-reset
1273 flag resets the terminal to the mode it had before
1274 .B interact
1275 was executed (invariably, cooked mode).
1276 Note that characters entered when the mode is being switched may be lost
1277 (an unfortunate feature of the terminal driver on some systems).
1278 The only reason to use
1279 .B \-reset
1280 is if your action
1281 depends on running in cooked mode.
1282 .IP
1283 The
1284 .B \-echo
1285 flag sends characters that match the following pattern back to the process
1286 that generated them as each character is read. This may be useful
1287 when the user needs to see feedback from partially typed patterns.
1288 .IP
1289 If a pattern is being echoed but eventually fails to match,
1290 the characters are sent to the spawned process. If the spawned
1291 process then echoes them, the user will see the characters twice.
1292 .B \-echo
1293 is probably only appropriate in situations where the user is
1294 unlikely to not complete the pattern. For example, the following
1295 excerpt is from rftp, the recursive-ftp script, where the user is
1296 prompted to enter ~g, ~p, or ~l, to get, put, or list the current
1297 directory recursively. These are so far away from the normal ftp
1298 commands, that the user is unlikely to type ~ followed by anything
1299 else, except mistakenly, in which case, they'll probably just ignore
1300 the result anyway.
1301 .nf
1302
1303 interact {
1304 -echo ~g {getcurdirectory 1}
1305 -echo ~l {getcurdirectory 0}
1306 -echo ~p {putcurdirectory}
1307 }
1308
1309 .fi
1310 The
1311 .B \-nobuffer
1312 flag sends characters that match the following pattern on to
1313 the output process as characters are read.
1314
1315 This is useful when you wish to let a program echo back the pattern.
1316 For example, the following might be used to monitor where a person is
1317 dialing (a Hayes-style modem). Each time "atd" is seen the script
1318 logs the rest of the line.
1319 .nf
1320
1321 proc lognumber {} {
1322 interact -nobuffer -re "(.*)\\r" return
1323 puts $log "[clock format [clock seconds]]: dialed $interact_out(1,string)"
1324 }
1325
1326 interact -nobuffer "atd" lognumber
1327
1328 .fi
1329 .IP
1330 During
1331 .BR interact ,
1332 previous use of
1333 .B log_user
1334 is ignored. In particular,
1335 .B interact
1336 will force its output to be logged (sent to the standard output)
1337 since it is presumed the user doesn't wish to interact blindly.
1338 .IP
1339 The
1340 .B \-o
1341 flag causes any following key-body pairs to be applied to the output of
1342 the current process.
1343 This can be useful, for example, when dealing with hosts that
1344 send unwanted characters during a telnet session.
1345 .IP
1346 By default,
1347 .B interact
1348 expects the user to be writing stdin and reading stdout of the
1349 .B Expect
1350 process
1351 itself.
1352 The
1353 .B \-u
1354 flag (for "user") makes
1355 .B interact
1356 look for the user as the process named by its argument
1357 (which must be a spawned id).
1358 .IP
1359 This allows two unrelated processes to be joined
1360 together without using an explicit loop. To aid in debugging, Expect
1361 diagnostics always go to stderr (or stdout for certain logging and
1362 debugging information). For the same reason, the
1363 .B interpreter
1364 command will read interactively from stdin.
1365 .IP
1366 For example, the following fragment creates a login process.
1367 Then it dials the user (not shown), and finally connects the two together.
1368 Of course, any process may be substituted for login.
1369 A shell, for example, would allow the user to work without supplying an
1370 account and password.
1371 .nf
1372
1373 spawn login
1374 set login $spawn_id
1375 spawn tip modem
1376 # dial back out to user
1377 # connect user to login
1378 interact \-u $login
1379
1380 .fi
1381 To send output to multiple processes, list each spawn id list prefaced by a
1382 .B \-output
1383 flag. Input for a group of output spawn ids may be determined
1384 by a spawn id list prefaced by a
1385 .B \-input
1386 flag. (Both
1387 .B \-input
1388 and
1389 .B \-output
1390 may take lists in the same form as the
1391 .B \-i
1392 flag in the
1393 .B expect
1394 command, except that any_spawn_id is not meaningful in
1395 .BR interact .)
1396 All following flags and
1397 strings (or patterns) apply to this input until another -input flag appears.
1398 If no
1399 .B \-input
1400 appears,
1401 .B \-output
1402 implies "\-input $user_spawn_id \-output".
1403 (Similarly, with patterns that do not have
1404 .BR \-input .)
1405 If one
1406 .B \-input
1407 is specified, it overrides $user_spawn_id. If a second
1408 .B \-input
1409 is specified,
1410 it overrides $spawn_id. Additional
1411 .B \-input
1412 flags may be specified.
1413
1414 The two implied input processes default to having their outputs specified as
1415 $spawn_id and $user_spawn_id (in reverse).
1416 If a
1417 .B \-input
1418 flag appears
1419 with no
1420 .B \-output
1421 flag, characters from that process are discarded.
1422
1423 The
1424 .B \-i
1425 flag introduces a replacement for the current spawn_id when no
1426 other
1427 .B \-input
1428 or
1429 .B \-output
1430 flags are used. A \-i flag implies a \-o flag.
1431
1432 It is possible to change the processes that are being interacted with
1433 by using indirect spawn ids. (Indirect spawn ids are described in the
1434 section on the expect command.) Indirect spawn ids may be specified
1435 with the -i, -u, -input, or -output flags.
1436 .TP
1437 .B interpreter " [args]"
1438 causes the user to be interactively prompted for
1439 .B Expect
1440 and Tcl commands.
1441 The result of each command is printed.
1442 .IP
1443 Actions such as
1444 .B break
1445 and
1446 .B continue
1447 cause control structures (i.e.,
1448 .BR for ,
1449 .BR proc )
1450 to behave in the usual way.
1451 However
1452 .B return
1453 causes interpreter to return to its caller, while
1454 .B inter_return
1455 causes
1456 .B interpreter
1457 to cause a return in its caller. For example, if "proc foo" called
1458 .B interpreter
1459 which then executed the action
1460 .BR inter_return ,
1461 .B proc foo
1462 would return.
1463 Any other command causes
1464 .B interpreter
1465 to continue prompting for new commands.
1466 .IP
1467 By default, the prompt contains two integers.
1468 The first integer describes the depth of
1469 the evaluation stack (i.e., how many times Tcl_Eval has been called). The
1470 second integer is the Tcl history identifier. The prompt can be set by
1471 defining a procedure called "prompt1" whose return value becomes the next
1472 prompt. If a statement has open quotes, parens, braces, or brackets, a
1473 secondary prompt (by default "+> ") is issued upon newline. The secondary
1474 prompt may be set by defining a procedure called "prompt2".
1475 .IP
1476 During
1477 .BR interpreter ,
1478 cooked mode is used, even if the its caller was using raw mode.
1479 .IP
1480 If stdin is closed,
1481 .B interpreter
1482 will return unless the
1483 .B \-eof
1484 flag is used, in which case the subsequent argument is invoked.
1485 .TP
1486 .BI log_file " [args] [[\-a] file]"
1487 If a filename is provided,
1488 .B log_file
1489 will record a transcript of the session (beginning at that point) in the file.
1490 .B log_file
1491 will stop recording if no argument is given. Any previous log file is closed.
1492
1493 Instead of a filename, a Tcl file identifier may be provided by using the
1494 .B \-open
1495 or
1496 .B \-leaveopen
1497 flags. This is similar to the
1498 .B spawn
1499 command. (See
1500 .B spawn
1501 for more info.)
1502
1503 The
1504 .B \-a
1505 flag forces output to be logged that was suppressed by the
1506 .B log_user
1507 command.
1508
1509 By default, the
1510 .B log_file
1511 command
1512 .I appends
1513 to old files rather than truncating them,
1514 for the convenience of being able to turn logging off and on multiple
1515 times in one session.
1516 To truncate files, use the
1517 .B \-noappend
1518 flag.
1519
1520 The
1521 .B -info
1522 flag causes log_file to return a description of the
1523 most recent non-info arguments given.
1524 .TP
1525 .BI log_user " -info|0|1"
1526 By default, the send/expect dialogue is logged to stdout
1527 (and a logfile if open).
1528 The logging to stdout is disabled by the command "log_user 0"
1529 and reenabled by "log_user 1". Logging to the logfile is unchanged.
1530
1531 The
1532 .B -info
1533 flag causes log_user to return a description of the
1534 most recent non-info arguments given.
1535 .TP
1536 .BI match_max " [\-d] [\-i spawn_id] [size]"
1537 defines the size of the buffer (in bytes) used internally by
1538 .BR expect .
1539 With no
1540 .I size
1541 argument, the current size is returned.
1542 .IP
1543 With the
1544 .B \-d
1545 flag, the default size is set. (The initial default is 2000.)
1546 With the
1547 .B \-i
1548 flag, the size is set for the named spawn id, otherwise it is set for
1549 the current process.
1550 .TP
1551 .BI overlay " [\-# spawn_id] [\-# spawn_id] [...] program [args]"
1552 executes
1553 .IR "program args"
1554 in place of the current
1555 .B Expect
1556 program, which terminates.
1557 A bare hyphen argument forces a hyphen in front of the command name as if
1558 it was a login shell.
1559 All spawn_ids are closed except for those named as arguments. These
1560 are mapped onto the named file identifiers.
1561 .IP
1562 Spawn_ids are mapped to file identifiers for the new program to inherit.
1563 For example, the following line runs chess and allows it to be
1564 controlled by the current process \- say, a chess master.
1565 .nf
1566
1567 overlay \-0 $spawn_id \-1 $spawn_id \-2 $spawn_id chess
1568
1569 .fi
1570 This is more efficient than
1571 "interact \-u", however, it sacrifices the ability to do programmed
1572 interaction since the
1573 .B Expect
1574 process is no longer in control.
1575 .IP
1576 Note that no controlling terminal is provided. Thus, if you
1577 disconnect or remap standard input, programs that do
1578 job control (shells, login, etc) will not function properly.
1579 .TP
1580 .BI parity " [\-d] [\-i spawn_id] [value]"
1581 defines whether parity should be retained or stripped from the output of
1582 spawned processes. If
1583 .I value
1584 is zero, parity is stripped, otherwise it is not stripped.
1585 With no
1586 .I value
1587 argument, the current value is returned.
1588 .IP
1589 With the
1590 .B \-d
1591 flag, the default parity value is set. (The initial default is 1, i.e.,
1592 parity is not stripped.)
1593 With the
1594 .B \-i
1595 flag, the parity value is set for the named spawn id, otherwise it is set for
1596 the current process.
1597 .TP
1598 .BI remove_nulls " [\-d] [\-i spawn_id] [value]"
1599 defines whether nulls are retained or removed from the output of
1600 spawned processes before pattern matching
1601 or storing in the variable
1602 .I expect_out
1603 or
1604 .IR interact_out .
1605 If
1606 .I value
1607 is 1, nulls are removed. If
1608 .I value
1609 is 0, nulls are not removed.
1610 With no
1611 .I value
1612 argument, the current value is returned.
1613 .IP
1614 With the
1615 .B \-d
1616 flag, the default value is set. (The initial default is 1, i.e.,
1617 nulls are removed.)
1618 With the
1619 .B \-i
1620 flag, the value is set for the named spawn id, otherwise it is set for
1621 the current process.
1622
1623 Whether or not nulls are removed,
1624 .B Expect
1625 will record null bytes to the log and stdout.
1626 .TP
1627 .BI send " [\-flags] string"
1628 Sends
1629 .IR string
1630 to the current process.
1631 For example, the command
1632 .nf
1633
1634 send "hello world\\r"
1635
1636 .fi
1637 sends the characters, h e l l o <blank> w o r l d <return> to the
1638 current process.
1639 (Tcl includes a printf-like command (called
1640 .BR format )
1641 which can build arbitrarily complex strings.)
1642 .IP
1643 Characters are sent immediately although programs with line-buffered input
1644 will not read the characters until a return character is sent. A return
1645 character is denoted "\\r".
1646
1647 The
1648 .B \-\-
1649 flag forces the next argument to be interpreted as a string rather than a flag.
1650 Any string can be preceded by "\-\-" whether or not it actually looks
1651 like a flag. This provides a reliable mechanism to specify variable strings
1652 without being tripped up by those that accidentally look like flags.
1653 (All strings starting with "-" are reserved for future options.)
1654
1655 The
1656 .B \-i
1657 flag declares that the string be sent to the named spawn_id.
1658 If the spawn_id is
1659 .IR user_spawn_id ,
1660 and the terminal is in raw mode, newlines in the string are translated
1661 to return-newline
1662 sequences so that they appear as if the terminal was in cooked mode.
1663 The
1664 .B \-raw
1665 flag disables this translation.
1666
1667 The
1668 .BR \-null
1669 flag sends null characters (0 bytes). By default, one null is sent.
1670 An integer may follow the
1671 .BR \-null
1672 to indicate how many nulls to send.
1673
1674 The
1675 .B \-break
1676 flag generates a break condition. This only makes sense if the spawn
1677 id refers to a tty device opened via "spawn -open". If you have
1678 spawned a process such as tip, you should use tip's convention for
1679 generating a break.
1680
1681 The
1682 .B \-s
1683 flag forces output to be sent "slowly", thus avoid the common situation
1684 where a computer outtypes an input buffer that was designed for a
1685 human who would never outtype the same buffer. This output is
1686 controlled by the value of the variable "send_slow" which takes a two
1687 element list. The first element is an integer that describes the
1688 number of bytes to send atomically. The second element is a real
1689 number that describes the number of seconds by which the atomic sends
1690 must be separated. For example, "set send_slow {10 .001}" would force
1691 "send \-s" to send strings with 1 millisecond in between each 10
1692 characters sent.
1693
1694 The
1695 .B \-h
1696 flag forces output to be sent (somewhat) like a human actually typing.
1697 Human-like delays appear between the characters. (The algorithm is
1698 based upon a Weibull distribution, with modifications to suit this
1699 particular application.) This output is controlled by the value of
1700 the variable "send_human" which takes a five element list. The first
1701 two elements are average interarrival time of characters in seconds.
1702 The first is used by default. The second is used at word endings, to
1703 simulate the subtle pauses that occasionally occur at such
1704 transitions. The third parameter is a measure of variability where .1
1705 is quite variable, 1 is reasonably variable, and 10 is quite
1706 invariable. The extremes are 0 to infinity. The last two parameters
1707 are, respectively, a minimum and maximum interarrival time.
1708 The minimum and maximum are used last and "clip" the final time.
1709 The ultimate average can be quite different from the given average
1710 if the minimum and maximum clip enough values.
1711
1712 As an
1713 example, the following command emulates a fast and
1714 consistent typist:
1715 .nf
1716
1717 set send_human {.1 .3 1 .05 2}
1718 send \-h "I'm hungry. Let's do lunch."
1719
1720 .fi
1721 while the following might be more suitable after a hangover:
1722 .nf
1723
1724 set send_human {.4 .4 .2 .5 100}
1725 send \-h "Goodd party lash night!"
1726
1727 .fi
1728 Note that errors are not simulated, although you can set up error
1729 correction situations yourself by embedding mistakes and corrections
1730 in a send argument.
1731
1732 The flags for sending null characters, for sending breaks, for forcing slow
1733 output and for human-style output are mutually exclusive. Only the one
1734 specified last will be used. Furthermore, no
1735 .I string
1736 argument can be specified with the flags for sending null characters or breaks.
1737
1738 It is a good idea to precede the first
1739 .B send
1740 to a process by an
1741 .BR expect .
1742 .B expect
1743 will wait for the process to start, while
1744 .B send
1745 cannot.
1746 In particular, if the first
1747 .B send
1748 completes before the process starts running,
1749 you run the risk of having your data ignored.
1750 In situations where interactive programs offer no initial prompt,
1751 you can precede
1752 .B send
1753 by a delay as in:
1754 .nf
1755
1756 # To avoid giving hackers hints on how to break in,
1757 # this system does not prompt for an external password.
1758 # Wait for 5 seconds for exec to complete
1759 spawn telnet very.secure.gov
1760 sleep 5
1761 send password\\r
1762
1763 .fi
1764 .B exp_send
1765 is an alias for
1766 .BI send .
1767 If you are using Expectk or some other variant of Expect in the Tk environment,
1768 .B send
1769 is defined by Tk for an entirely different purpose.
1770 .B exp_send
1771 is provided for compatibility between environments.
1772 Similar aliases are provided for other Expect's other send commands.
1773 .TP
1774 .BI send_error " [\-flags] string"
1775 is like
1776 .BR send ,
1777 except that the output is sent to stderr rather than the current
1778 process.
1779 .TP
1780 .BI send_log " [\--] string"
1781 is like
1782 .BR send ,
1783 except that the string is only sent to the log file (see
1784 .BR log_file .)
1785 The arguments are ignored if no log file is open.
1786 .TP
1787 .BI send_tty " [\-flags] string"
1788 is like
1789 .BR send ,
1790 except that the output is sent to /dev/tty rather than the current
1791 process.
1792 .TP
1793 .BI send_user " [\-flags] string"
1794 is like
1795 .BR send ,
1796 except that the output is sent to stdout rather than the current
1797 process.
1798 .TP
1799 .BI sleep " seconds"
1800 causes the script to sleep for the given number of seconds.
1801 Seconds may be a decimal number. Interrupts (and Tk events if you
1802 are using Expectk) are processed while Expect sleeps.
1803 .TP
1804 .BI spawn " [args] program [args]"
1805 creates a new process running
1806 .IR "program args" .
1807 Its stdin, stdout and stderr are connected to Expect,
1808 so that they may be read and written by other
1809 .B Expect
1810 commands.
1811 The connection is broken by
1812 .B close
1813 or if the process itself closes any of the file identifiers.
1814 .IP
1815 When a process is started by
1816 .BR spawn ,
1817 the variable
1818 .I spawn_id
1819 is set to a descriptor referring to that process.
1820 The process described by
1821 .I spawn_id
1822 is considered the
1823 .IR "current process" .
1824 .I spawn_id
1825 may be read or written, in effect providing job control.
1826 .IP
1827 .I user_spawn_id
1828 is a global variable containing a descriptor which refers to the user.
1829 For example, when
1830 .I spawn_id
1831 is set to this value,
1832 .B expect
1833 behaves like
1834 .BR expect_user .
1835
1836 .I
1837 .I error_spawn_id
1838 is a global variable containing a descriptor which refers to the standard
1839 error.
1840 For example, when
1841 .I spawn_id
1842 is set to this value,
1843 .B send
1844 behaves like
1845 .BR send_error .
1846 .IP
1847 .I tty_spawn_id
1848 is a global variable containing a descriptor which refers to /dev/tty.
1849 If /dev/tty does not exist (such as in a cron, at, or batch script), then
1850 .I tty_spawn_id
1851 is not defined. This may be tested as:
1852 .nf
1853
1854 if {[info vars tty_spawn_id]} {
1855 # /dev/tty exists
1856 } else {
1857 # /dev/tty doesn't exist
1858 # probably in cron, batch, or at script
1859 }
1860
1861 .fi
1862 .IP
1863 .B spawn
1864 returns the UNIX process id. If no process is spawned, 0 is returned.
1865 The variable
1866 .I spawn_out(slave,name)
1867 is set to the name of the pty slave device.
1868 .IP
1869 By default,
1870 .B spawn
1871 echoes the command name and arguments. The
1872 .B \-noecho
1873 flag stops
1874 .B spawn
1875 from doing this.
1876 .IP
1877 The
1878 .B \-console
1879 flag causes console output to be redirected to the spawned process.
1880 This is not supported on all systems.
1881
1882 Internally,
1883 .B spawn
1884 uses a pty, initialized the same way as the user's tty. This is further
1885 initialized so that all settings are "sane" (according to stty(1)).
1886 If the variable
1887 .I stty_init
1888 is defined, it is interpreted in the style of stty arguments
1889 as further configuration.
1890 For example, "set stty_init raw" will cause further spawned processes's
1891 terminals to start in raw mode.
1892 .B \-nottycopy
1893 skips the initialization based on the user's tty.
1894 .B \-nottyinit
1895 skips the "sane" initialization.
1896 .IP
1897 Normally,
1898 .B spawn
1899 takes little time to execute. If you notice spawn taking a
1900 significant amount of time, it is probably encountering ptys that are
1901 wedged. A number of tests are run on ptys to avoid entanglements with
1902 errant processes. (These take 10 seconds per wedged pty.) Running
1903 Expect with the
1904 .B \-d
1905 option will show if
1906 .B Expect
1907 is encountering many ptys in odd states. If you cannot kill
1908 the processes to which these ptys are attached, your only recourse may
1909 be to reboot.
1910
1911 If
1912 .I program
1913 cannot be spawned successfully because exec(2) fails (e.g. when
1914 .I program
1915 doesn't exist), an error message will be returned by the next
1916 .B interact
1917 or
1918 .B expect
1919 command as if
1920 .I program
1921 had run and produced the error message as output.
1922 This behavior is a natural consequence of the implementation of
1923 .BR spawn .
1924 Internally, spawn forks, after which the spawned process has no
1925 way to communicate with the original
1926 .B Expect
1927 process except by communication
1928 via the spawn_id.
1929
1930 The
1931 .B \-open
1932 flag causes the next argument to be interpreted as a Tcl file identifier
1933 (i.e., returned by
1934 .BR open .)
1935 The spawn id can then be used as if it were a spawned process. (The file
1936 identifier should no longer be used.)
1937 This lets you treat raw devices, files, and
1938 pipelines as spawned processes without using a pty. 0 is returned to
1939 indicate there is no associated process. When the connection to
1940 the spawned process is closed, so is the Tcl file identifier.
1941 The
1942 .B \-leaveopen
1943 flag is similar to
1944 .B \-open
1945 except that
1946 .B \-leaveopen
1947 causes the file identifier to be left open even after the spawn id is closed.
1948
1949 The
1950 .B \-pty
1951 flag causes a pty to be opened but no process spawned. 0 is returned
1952 to indicate there is no associated process. Spawn_id is set as usual.
1953
1954 The variable
1955 .I spawn_out(slave,fd)
1956 is set to a file identifier corresponding to the pty slave.
1957 It can be closed using "close -slave".
1958
1959 The
1960 .B \-ignore
1961 flag names a signal to be ignored in the spawned process.
1962 Otherwise, signals get the default behavior.
1963 Signals are named as in the
1964 .B trap
1965 command, except that each signal requires a separate flag.
1966 .TP
1967 .BI strace " level"
1968 causes following statements to be printed before being executed.
1969 (Tcl's trace command traces variables.)
1970 .I level
1971 indicates how far down in the call stack to trace.
1972 For example,
1973 the following command runs
1974 .B Expect
1975 while tracing the first 4 levels of calls,
1976 but none below that.
1977 .nf
1978
1979 expect \-c "strace 4" script.exp
1980
1981 .fi
1982
1983 The
1984 .B -info
1985 flag causes strace to return a description of the
1986 most recent non-info arguments given.
1987 .TP
1988 .BI stty " args"
1989 changes terminal modes similarly to the external stty command.
1990
1991 By default, the controlling terminal is accessed. Other terminals can
1992 be accessed by appending "< /dev/tty..." to the command. (Note that
1993 the arguments should not be grouped into a single argument.)
1994
1995 Requests for status return it as the result of the command. If no status
1996 is requested and the controlling terminal is accessed, the previous
1997 status of the raw and echo attributes are returned in a form which can
1998 later be used by the command.
1999
2000 For example, the arguments
2001 .B raw
2002 or
2003 .B \-cooked
2004 put the terminal into raw mode.
2005 The arguments
2006 .B \-raw
2007 or
2008 .B cooked
2009 put the terminal into cooked mode.
2010 The arguments
2011 .B echo
2012 and
2013 .B \-echo
2014 put the terminal into echo and noecho mode respectively.
2015 .IP
2016 The following example illustrates how to temporarily disable echoing.
2017 This could be used in otherwise-automatic
2018 scripts to avoid embedding passwords in them.
2019 (See more discussion on this under EXPECT HINTS below.)
2020 .nf
2021
2022 stty \-echo
2023 send_user "Password: "
2024 expect_user -re "(.*)\\n"
2025 set password $expect_out(1,string)
2026 stty echo
2027
2028 .fi
2029 .TP
2030 .BI system " args"
2031 gives
2032 .I args
2033 to sh(1) as input,
2034 just as if it had been typed as a command from a terminal.
2035 .B Expect
2036 waits until the shell terminates.
2037 The return status from sh is handled the same way that
2038 .B exec
2039 handles its return status.
2040 .IP
2041 In contrast to
2042 .B exec
2043 which redirects stdin and stdout to the script,
2044 .B system
2045 performs no redirection
2046 (other than that indicated by the string itself).
2047 Thus, it is possible to use programs which must talk directly to /dev/tty.
2048 For the same reason, the results of
2049 .B system
2050 are not recorded in the log.
2051 .TP
2052 .BI timestamp " [args]"
2053 returns a timestamp.
2054 With no arguments, the number of
2055 seconds since the epoch is returned.
2056
2057 The
2058 .B \-format
2059 flag introduces a string which is returned but with
2060 substitutions made according to the
2061 POSIX rules for strftime. For example %a is replaced by an abbreviated
2062 weekday name (i.e., Sat). Others are:
2063 .nf
2064 %a abbreviated weekday name
2065 %A full weekday name
2066 %b abbreviated month name
2067 %B full month name
2068 %c date-time as in: Wed Oct 6 11:45:56 1993
2069 %d day of the month (01-31)
2070 %H hour (00-23)
2071 %I hour (01-12)
2072 %j day (001-366)
2073 %m month (01-12)
2074 %M minute (00-59)
2075 %p am or pm
2076 %S second (00-61)
2077 %u day (1-7, Monday is first day of week)
2078 %U week (00-53, first Sunday is first day of week one)
2079 %V week (01-53, ISO 8601 style)
2080 %w day (0-6)
2081 %W week (00-53, first Monday is first day of week one)
2082 %x date-time as in: Wed Oct 6 1993
2083 %X time as in: 23:59:59
2084 %y year (00-99)
2085 %Y year as in: 1993
2086 %Z timezone (or nothing if not determinable)
2087 %% a bare percent sign
2088
2089 .fi
2090 Other % specifications are undefined. Other characters will be passed
2091 through untouched. Only the C locale is supported.
2092
2093 The
2094 .B \-seconds
2095 flag introduces a number of seconds since the epoch to be used as a source
2096 from which to format. Otherwise, the current time is used.
2097
2098 The
2099 .B \-gmt
2100 flag forces timestamp output to use the GMT timezone. With no flag,
2101 the local timezone is used.
2102 .TP
2103 .BI trap " [[command] signals]"
2104 causes the given
2105 .I command
2106 to be executed upon future receipt of any of the given signals.
2107 The command is executed in the global scope.
2108 If
2109 .I command
2110 is absent, the signal action is returned.
2111 If
2112 .I command
2113 is the string SIG_IGN, the signals are ignored.
2114 If
2115 .I command
2116 is the string SIG_DFL, the signals are result to the system default.
2117 .I signals
2118 is either a single signal or a list of signals. Signals may be specified
2119 numerically or symbolically as per signal(3). The "SIG" prefix may be omitted.
2120
2121 With no arguments (or the argument \-number),
2122 .B trap
2123 returns the signal number of the trap command currently being executed.
2124
2125 The
2126 .B \-code
2127 flag uses the return code of the command in place of whatever code Tcl
2128 was about to return when the command originally started running.
2129
2130 The
2131 .B \-interp
2132 flag causes the command to be evaluated using the interpreter
2133 active at the time the command started running
2134 rather than when the trap was declared.
2135
2136 The
2137 .B \-name
2138 flag causes the
2139 .B trap
2140 command to return the signal name of the trap command currently being executed.
2141
2142 The
2143 .B \-max
2144 flag causes the
2145 .B trap
2146 command to return the largest signal number that can be set.
2147
2148 For example, the command "trap {send_user "Ouch!"} SIGINT" will print "Ouch!"
2149 each time the user presses ^C.
2150
2151 By default, SIGINT (which can usually be generated by pressing ^C) and
2152 SIGTERM cause Expect to exit. This is due to the following trap, created
2153 by default when Expect starts.
2154 .nf
2155
2156 trap exit {SIGINT SIGTERM}
2157
2158 .fi
2159 If you use the -D flag to start the debugger, SIGINT is redefined
2160 to start the interactive debugger. This is due to the following trap:
2161 .nf
2162
2163 trap {exp_debug 1} SIGINT
2164
2165 .fi
2166 The debugger trap can be changed by setting the environment variable
2167 EXPECT_DEBUG_INIT to a new trap command.
2168
2169 You can, of course, override both of these just by adding trap
2170 commands to your script. In particular, if you have your own "trap
2171 exit SIGINT", this will override the debugger trap. This is useful
2172 if you want to prevent users from getting to the debugger at all.
2173
2174 If you want to define your own trap on SIGINT but still trap to the
2175 debugger when it is running, use:
2176 .nf
2177
2178 if {![exp_debug]} {trap mystuff SIGINT}
2179
2180 .fi
2181 Alternatively, you can trap to the debugger using some other signal.
2182
2183 .B trap
2184 will not let you override the action for SIGALRM as this is used internally
2185 to
2186 .BR Expect .
2187 The disconnect command sets SIGALRM to SIG_IGN (ignore). You can reenable
2188 this as long as you disable it during subsequent spawn commands.
2189
2190 See signal(3) for more info.
2191 .TP
2192 .BI wait " [args]"
2193 delays until a spawned process (or
2194 the current process if none is named) terminates.
2195 .IP
2196 .B wait
2197 normally returns a list of four integers.
2198 The first integer is the pid of the process that was waited upon.
2199 The second integer is the corresponding spawn id.
2200 The third integer is -1 if an operating system error occurred, or 0 otherwise.
2201 If the third integer was 0, the fourth integer is the status returned by
2202 the spawned process. If the third integer was -1, the fourth integer is
2203 the value of errno set by the operating system. The global variable
2204 errorCode is also set.
2205
2206 Additional elements may appear at the end of the return value from
2207 .BR wait .
2208 An optional fifth element identifies a class of information.
2209 Currently, the only possible value for this element is CHILDKILLED in
2210 which case the next two values are the C-style signal name and a short
2211 textual description.
2212 .IP
2213 The
2214 .B \-i
2215 flag declares the process to wait corresponding to the named spawn_id
2216 (NOT the process id).
2217 Inside a SIGCHLD handler,
2218 it is possible to wait for any spawned process by using the spawn id -1.
2219
2220 The
2221 .B \-nowait
2222 flag causes the wait to return immediately with the indication of a
2223 successful wait. When the process exits (later), it will automatically
2224 disappear without the need for an explicit wait.
2225
2226 The
2227 .B wait
2228 command may also be used wait for a forked process using the arguments
2229 "-i -1". Unlike its use with spawned processes, this command can be
2230 executed at any time. There is no control over which process is
2231 reaped. However, the return value can be checked for the process id.
2232
2233 .SH LIBRARIES
2234 Expect automatically knows about two built-in libraries for Expect scripts.
2235 These are defined by the directories named in the variables
2236 exp_library and exp_exec_library. Both are meant to contain utility
2237 files that can be used by other scripts.
2238
2239 exp_library contains architecture-independent files. exp_exec_library
2240 contains architecture-dependent files. Depending on your system, both
2241 directories may be totally empty. The existence of the file
2242 $exp_exec_library/cat-buffers describes whether your /bin/cat buffers
2243 by default.
2244 .SH PRETTY-PRINTING
2245 A vgrind definition is available for pretty-printing
2246 .B Expect
2247 scripts.
2248 Assuming the vgrind definition supplied with the
2249 .B Expect
2250 distribution is
2251 correctly installed, you can use it as:
2252 .nf
2253
2254 vgrind \-lexpect file
2255
2256 .fi
2257 .SH EXAMPLES
2258 It many not be apparent how to put everything together that the man page
2259 describes. I encourage you to read and try out the examples in
2260 the example directory of the
2261 .B Expect
2262 distribution.
2263 Some of them are real programs. Others are simply illustrative
2264 of certain techniques, and of course, a couple are just quick hacks.
2265 The INSTALL file has a quick overview of these programs.
2266 .PP
2267 The
2268 .B Expect
2269 papers (see SEE ALSO) are also useful. While some papers
2270 use syntax corresponding to earlier versions of Expect, the accompanying
2271 rationales are still valid and go into a lot more detail than this
2272 man page.
2273 .SH CAVEATS
2274 Extensions may collide with Expect's command names. For example,
2275 .B send
2276 is defined by Tk for an entirely different purpose.
2277 For this reason, most of the
2278 .B Expect
2279 commands are also available as "exp_XXXX".
2280 Commands and variables beginning with "exp", "inter", "spawn",
2281 and "timeout" do not have aliases.
2282 Use the extended command names if you need this compatibility between environments.
2283
2284 .B Expect
2285 takes a rather liberal view of scoping.
2286 In particular, variables read by commands specific to the
2287 .B Expect
2288 program will be sought first from the local scope, and if not found, in the
2289 global scope. For example, this
2290 obviates the need to place "global timeout" in every
2291 procedure you write that uses
2292 .BR expect .
2293 On the other hand, variables written are always in the local scope (unless
2294 a "global" command has been issued). The most common problem this causes
2295 is when spawn is executed in a procedure. Outside the procedure,
2296 .I spawn_id
2297 no longer exists, so the spawned process is no longer accessible
2298 simply because of scoping. Add a "global spawn_id" to such a procedure.
2299
2300 If you cannot enable the multispawning capability
2301 (i.e., your system supports neither select (BSD *.*), poll (SVR>2),
2302 nor something equivalent),
2303 .B Expect
2304 will only be able to control a single process at a time.
2305 In this case, do not attempt to set
2306 .IR spawn_id ,
2307 nor should you execute processes via exec while a spawned process
2308 is running. Furthermore, you will not be able to
2309 .B expect
2310 from multiple processes (including the user as one) at the same time.
2311
2312 Terminal parameters can have a big effect on scripts. For example, if
2313 a script is written to look for echoing, it will misbehave if echoing
2314 is turned off. For this reason, Expect forces sane terminal
2315 parameters by default. Unfortunately, this can make things unpleasant
2316 for other programs. As an example, the emacs shell wants to change
2317 the "usual" mappings: newlines get mapped to newlines instead of
2318 carriage-return newlines, and echoing is disabled. This allows one to
2319 use emacs to edit the input line. Unfortunately, Expect cannot
2320 possibly guess this.
2321
2322 You can request that Expect not override its default setting of
2323 terminal parameters, but you must then be very careful when writing
2324 scripts for such environments. In the case of emacs, avoid depending
2325 upon things like echoing and end-of-line mappings.
2326
2327 The commands that accepted arguments braced into a single list (the
2328 .B expect
2329 variants and
2330 .BR interact )
2331 use a heuristic to decide if the list is actually one argument or
2332 many. The heuristic can fail only in the case when the list actually
2333 does represent a single argument which has multiple embedded \\n's
2334 with non-whitespace characters between them. This seems sufficiently
2335 improbable, however the argument "\-nobrace" can be used to force a
2336 single argument to be handled as a single argument. This could
2337 conceivably be used with machine-generated Expect code. Similarly,
2338 -brace forces a single argument to be handle as multiple patterns/actions.
2339
2340 .SH BUGS
2341 It was really tempting to name the program "sex" (for either "Smart EXec"
2342 or "Send-EXpect"), but good sense (or perhaps just Puritanism) prevailed.
2343
2344 On some systems, when a shell is spawned, it complains about not being
2345 able to access the tty but runs anyway. This means your system has a
2346 mechanism for gaining the controlling tty that
2347 .B Expect
2348 doesn't know about. Please find out what it is, and send this information
2349 back to me.
2350
2351 Ultrix 4.1 (at least the latest versions around here) considers
2352 timeouts of above 1000000 to be equivalent to 0.
2353
2354 Digital UNIX 4.0A (and probably other versions) refuses to allocate
2355 ptys if you define a SIGCHLD handler. See grantpt page for more info.
2356
2357 IRIX 6.0 does not handle pty permissions correctly so that if Expect
2358 attempts to allocate a pty previously used by someone else, it fails.
2359 Upgrade to IRIX 6.1.
2360
2361 Telnet (verified only under SunOS 4.1.2) hangs if TERM is not set.
2362 This is a problem under cron, at and in cgi scripts, which do not
2363 define TERM. Thus, you must set it explicitly - to what type is
2364 usually irrelevant. It just has to be set to something! The
2365 following probably suffices for most cases.
2366 .nf
2367
2368 set env(TERM) vt100
2369
2370 .fi
2371
2372 Tip (verified only under BSDI BSD/OS 3.1 i386) hangs if SHELL and HOME
2373 are not set. This is a problem under cron, at and in cgi scripts,
2374 which do not define these environment variables. Thus, you must set
2375 them explicitly - to what type is usually irrelevant. It just has to
2376 be set to something! The following probably suffices for most cases.
2377 .nf
2378
2379 set env(SHELL) /bin/sh
2380 set env(HOME) /usr/local/bin
2381
2382 .fi
2383
2384
2385 Some implementations of ptys are designed so that the kernel throws
2386 away any unread output after 10 to 15 seconds (actual number is
2387 implementation-dependent) after the process has closed the file
2388 descriptor. Thus
2389 .B Expect
2390 programs such as
2391 .nf
2392
2393 spawn date
2394 sleep 20
2395 expect
2396
2397 .fi
2398 will fail. To avoid this, invoke non-interactive programs with
2399 .B exec
2400 rather than
2401 .BR spawn .
2402 While such situations are conceivable, in practice I have never
2403 encountered a situation in which the final output of a truly
2404 interactive program would be lost due to this behavior.
2405
2406 On the other hand, Cray UNICOS ptys throw away any unread output
2407 immediately after the process has closed the file descriptor. I have
2408 reported this to Cray and they are working on a fix.
2409
2410 Sometimes a delay is required between a prompt and a response, such as
2411 when a tty interface is changing UART settings or matching baud rates
2412 by looking for start/stop bits. Usually, all this is require is to
2413 sleep for a second or two. A more robust technique is to retry until
2414 the hardware is ready to receive input. The following example uses
2415 both strategies:
2416 .nf
2417
2418 send "speed 9600\\r";
2419 sleep 1
2420 expect {
2421 timeout {send "\\r"; exp_continue}
2422 $prompt
2423 }
2424
2425 .fi
2426
2427 trap \-code will not work with any command that sits in Tcl's event
2428 loop, such as sleep. The problem is that in the event loop, Tcl
2429 discards the return codes from async event handlers. A workaround is
2430 to set a flag in the trap code. Then check the flag immediately after
2431 the command (i.e., sleep).
2432
2433 The expect_background command ignores -timeout arguments and has no
2434 concept of timeouts in general.
2435
2436 .SH "EXPECT HINTS"
2437 There are a couple of things about
2438 .B Expect
2439 that may be non-intuitive.
2440 This section attempts to address some of these things with a couple of
2441 suggestions.
2442
2443 A common expect problem is how to recognize shell prompts. Since
2444 these are customized differently by differently people and different
2445 shells, portably automating rlogin can be difficult without knowing
2446 the prompt. A reasonable convention is to have users store a regular
2447 expression describing their prompt (in particular, the end of it) in
2448 the environment variable EXPECT_PROMPT. Code like the following
2449 can be used. If EXPECT_PROMPT doesn't exist, the code still has a good chance of functioning correctly.
2450 .nf
2451
2452 set prompt "(%|#|\\\\$) $" ;# default prompt
2453 catch {set prompt $env(EXPECT_PROMPT)}
2454
2455 expect -re $prompt
2456
2457 .fi
2458 I encourage you to write
2459 .B expect
2460 patterns that include the end of whatever
2461 you expect to see. This avoids the possibility of answering a question
2462 before seeing the entire thing. In addition, while you may well be
2463 able to answer questions before seeing them entirely, if you answer
2464 early, your answer may appear echoed back in the middle of the question.
2465 In other words, the resulting dialogue will be correct but look scrambled.
2466
2467 Most prompts include a space character at the end.
2468 For example, the prompt from ftp is 'f', 't', 'p', '>' and <blank>.
2469 To match this prompt, you must account for each of these characters.
2470 It is a common mistake not to include the blank.
2471 Put the blank in explicitly.
2472
2473 If you use a pattern of the form X*, the * will match all the output
2474 received from the end of X to the last thing received.
2475 This sounds intuitive but can be somewhat confusing because the phrase
2476 "last thing received" can vary depending upon the speed of the computer
2477 and the processing of I/O both by the kernel and the device driver.
2478 .PP
2479 In particular, humans tend to see program output arriving in huge chunks
2480 (atomically) when in reality most programs produce output one
2481 line at a time. Assuming this is the case, the * in the pattern of the
2482 previous paragraph may only match the end of the current line even though
2483 there seems to be more, because at the time of the match that was all
2484 the output that had been received.
2485 .PP
2486 .B expect
2487 has no way of knowing that further output is coming unless your
2488 pattern specifically accounts for it.
2489 .PP
2490 Even depending on line-oriented buffering is unwise. Not only do programs
2491 rarely make promises about the type of buffering they do, but system
2492 indigestion can break output lines up so that lines break at seemingly
2493 random places. Thus, if you can express the last few characters
2494 of a prompt when writing patterns, it is wise to do so.
2495
2496 If you are waiting for a pattern in the last output of a program
2497 and the program emits something else instead, you will not be able to
2498 detect that with the
2499 .B timeout
2500 keyword. The reason is that
2501 .B expect
2502 will not timeout \- instead it will get an
2503 .B eof
2504 indication.
2505 Use that instead. Even better, use both. That way if that line
2506 is ever moved around, you won't have to edit the line itself.
2507
2508 Newlines are usually converted to carriage return, linefeed sequences
2509 when output by the terminal driver. Thus, if you want a pattern that
2510 explicitly matches the two lines, from, say, printf("foo\\nbar"),
2511 you should use the pattern "foo\\r\\nbar".
2512 .PP
2513 A similar translation occurs when reading from the user, via
2514 .BR expect_user .
2515 In this case, when you press return, it will be
2516 translated to a newline. If
2517 .B Expect
2518 then passes that to a program
2519 which sets its terminal to raw mode (like telnet), there is going to
2520 be a problem, as the program expects a true return. (Some programs
2521 are actually forgiving in that they will automatically translate
2522 newlines to returns, but most don't.) Unfortunately, there is no way to find
2523 out that a program put its terminal into raw mode.
2524 .PP
2525 Rather than manually replacing newlines with returns, the solution is to
2526 use the command "stty raw", which will stop the translation.
2527 Note, however, that this means that you will no longer get the cooked
2528 line-editing features.
2529 .PP
2530 .B interact
2531 implicitly sets your terminal to raw mode so this problem will not arise then.
2532
2533 It is often useful to store passwords (or other private information)
2534 in
2535 .B Expect
2536 scripts. This is not recommended since anything that is
2537 stored on a computer is susceptible to being accessed by anyone.
2538 Thus, interactively prompting for passwords from a script is a smarter
2539 idea than embedding them literally. Nonetheless, sometimes such embedding
2540 is the only possibility.
2541 .PP
2542 Unfortunately, the UNIX file system has no direct way of creating
2543 scripts which are executable but unreadable. Systems which support
2544 setgid shell scripts may indirectly simulate this as follows:
2545 .PP
2546 Create the
2547 .B Expect
2548 script (that contains the secret data) as usual.
2549 Make its permissions be 750 (\-rwxr\-x\-\-\-) and owned by a trusted group,
2550 i.e., a group which is allowed to read it. If necessary, create a new
2551 group for this purpose. Next, create a /bin/sh script with
2552 permissions 2751 (\-rwxr\-s\-\-x) owned by the same group as before.
2553 .PP
2554 The result is a script which may be executed (and read) by anyone.
2555 When invoked, it runs the
2556 .B Expect
2557 script.
2558 .SH "SEE ALSO"
2559 .BR Tcl (3),
2560 .BR libexpect (3)
2561 .br
2562 .I
2563 "Exploring Expect: A Tcl-Based Toolkit for Automating Interactive Programs"
2564 \fRby Don Libes, pp. 602, ISBN 1-56592-090-2, O'Reilly and Associates, 1995.
2565 .br
2566 .I
2567 "expect: Curing Those Uncontrollable Fits of Interactivity" \fRby Don Libes,
2568 Proceedings of the Summer 1990 USENIX Conference,
2569 Anaheim, California, June 11-15, 1990.
2570 .br
2571 .I
2572 "Using
2573 .B expect
2574 to Automate System Administration Tasks" \fRby Don Libes,
2575 Proceedings of the 1990 USENIX Large Installation Systems Administration
2576 Conference, Colorado Springs, Colorado, October 17-19, 1990.
2577 .br
2578 .I
2579 "Tcl: An Embeddable Command Language" \fRby John Ousterhout,
2580 Proceedings of the Winter 1990 USENIX Conference,
2581 Washington, D.C., January 22-26, 1990.
2582 .br
2583 .I
2584 "expect: Scripts for Controlling Interactive Programs" \fRby Don Libes,
2585 Computing Systems, Vol. 4, No. 2, University of California Press Journals,
2586 November 1991.
2587 .br
2588 .I
2589 "Regression Testing and Conformance Testing Interactive Programs", \fRby Don
2590 Libes, Proceedings of the Summer 1992 USENIX Conference, pp. 135-144,
2591 San Antonio, TX, June 12-15, 1992.
2592 .br
2593 .I
2594 "Kibitz \- Connecting Multiple Interactive Programs Together", \fRby Don Libes,
2595 Software \- Practice & Experience, John Wiley & Sons, West Sussex, England,
2596 Vol. 23, No. 5, May, 1993.
2597 .br
2598 .I
2599 "A Debugger for Tcl Applications", \fRby Don Libes,
2600 Proceedings of the 1993 Tcl/Tk Workshop, Berkeley, CA, June 10-11, 1993.
2601 .SH AUTHOR
2602 Don Libes, National Institute of Standards and Technology
2603 .SH ACKNOWLEDGMENTS
2604 Thanks to John Ousterhout for Tcl, and Scott Paisley for inspiration.
2605 Thanks to Rob Savoye for Expect's autoconfiguration code.
2606 .PP
2607 The HISTORY file documents much of the evolution of
2608 .BR expect .
2609 It makes interesting reading and might give you further insight to this
2610 software. Thanks to the people mentioned in it who sent me bug fixes
2611 and gave other assistance.
2612 .PP
2613 Design and implementation of
2614 .B Expect
2615 was paid for in part by the U.S. government and is therefore in the public
2616 domain.
2617 However the author and NIST would like credit
2618 if this program and documentation or portions of them are used.