📄 scripts.ps
字号:
0.11 ( is typed, a procedure called) 118.14 520.5 P5 11 Q0.11 (yes) 256.34 520.5 P0 12 Q0.11 ( is invoked. This could further automate the) 273.43 520.5 P2 F0.11 (fsck) 490.26 520.5 P0 F0.11 ( inter-) 510.92 520.5 P-0.25 (action just described, so that the user does not have to explicitly start the interpreter and type) 72 506.5 P4 F-0.6 (yes) 515.41 506.5 P0 F-0.25 (.) 537 506.5 P4 F0.35 (~a) 72 492.5 P0 F0.15 ( invokes a more complex action. When typed,) 86.39 492.5 P4 F0.35 (hello world) 313.89 492.5 P0 F0.15 ( is seen at the terminal and the) 393.4 492.5 P-0.21 (current date is sent to the process as if the user had typed it. The other pairs exit or suspend an) 72 478.5 P2 F-0.21 (ex-) 524.68 478.5 P0.92 (pect) 72 464.5 P0 F0.92 ( session while interacting with a spawned process. \050W) 93.31 464.5 P0.92 (ith no map, the characters would be) 363.6 464.5 P-0.08 (passed uninterpreted to the current process.\051 Appropriate maps can simulate) 72 450.5 P2 F-0.08 (csh) 441.54 450.5 P0 F-0.08 (-style job control) 458.2 450.5 P-0.14 (or much fancier actions. For instance,) 72 436.5 P5 11 Q-0.14 (^Z) 257.9 436.5 P0 12 Q-0.14 ( could pass control to the interactive) 269.77 436.5 P2 F-0.14 (expect) 445.69 436.5 P0 F-0.14 ( interpreter \320) 478.33 436.5 P-0.14 (analogous to what) 72 422.5 P5 11 Q-0.15 (^Z) 161.84 422.5 P0 12 Q-0.14 ( does in the shell \320 or it could change jobs to a spawned shell and resume the) 173.71 422.5 P(interaction.) 72 408.5 T0.36 (An unrealistic but amusing application of character mapping is the following script which runs a) 72 384.5 P(shell with a Dvorak keyboard. For brevity) 72 370.5 T(, only lowercase letters are mapped.) 275.74 370.5 T4 F(proc dvorak {} {) 115.2 337 T(interact ~q {return continue} ~d {} \134) 162 323.5 T(q {send \325} w {send ,} e {send .} \134) 207 310 T(r {send p} t {send y} y {send f} \134) 207 296.5 T(u {send g} i {send c} o {send r} \134) 207 283 T(p {send l} s {send o} d {send e} \134) 207 269.5 T(f {send u} g {send i} h {send d} \134) 207 256 T(j {send h} k {send t} l {send n} \134) 207 242.5 T(x {send q} c {send j} v {send k} \134) 207 229 T(b {send x} n {send b} , {send w} \134) 207 215.5 T(. {send v} / {send z} \325 {send -} \134) 207 202 T(\134; {send s} z {send \134;} \134) 207 188.5 T(}) 115.2 175 T(log_user 0) 115.2 148 T(scan [exec printenv SHELL] "%s" shell) 115.2 134.5 T(spawn $shell) 115.2 121 T(log_user 1) 115.2 107.5 T(send_user "~d for dvorak, ~q for qwerty \050default\051\134n") 115.2 94 T(send_user "Enter ~ sequences using qwerty keys\134n") 115.2 80.5 T(interact ~d dvorak ~q {}) 115.2 67 TFMENDPAGE%%EndPage: "14" 13%%Page: "13" 13612 792 0 FMBEGINPAGE72 740.81 540 756 R7 X0 KV72 36 540 47.95 RV0 10 Q0 X(expect: Scripts for Controlling Interactive Processes) 295.54 39.95 T0 12 Q(13) 528.01 39.95 T72 63 540 720 R7 XV0 X0.47 (Once the timeout is set and the program is spawned,) 72 712 P5 11 Q0.48 (expect) 330.17 712 P0 12 Q0.47 ( waits for either an end-of-\336le or the) 362.53 712 P0.61 (20 seconds to pass. If the end-of-\336le is seen, the program has \050almost certainly\051 exited, and) 72 698 P5 11 Q0.62 (ex-) 524.74 698 P-0.11 (pect) 72 684 P0 12 Q-0.11 ( returns. If the timeout has passed,) 92.76 684 P5 11 Q-0.11 (expect) 262.44 684 P0 12 Q-0.11 ( returns. In either case) 294.81 684 P2 F-0.11 (expect) 406.39 684 P0 F-0.11 ( exits, implicitly kill-) 439.03 684 P(ing the current process.) 72 670 T0.05 (It is educational to try and solve these last two examples without using) 72 646 P2 F0.05 (expect) 414.83 646 P0 F0.05 (. In both cases, the) 447.46 646 P0.78 (usual approach is to fork a second process that sleeps and then signals the original shell. If the) 72 632 P1.23 (process or read \336nishes \336rst, the shell kills the sleeper) 72 618 P1.23 (. Passing pids and preventing the back-) 340.87 618 P0.33 (ground process start message is a stumbling block for all but the most expert shell programmers.) 72 604 P0.56 (Providing a general approach to starting multiple processes this way complicates the shell script) 72 590 P(immensely) 72 576 T(. Invariably) 123.86 576 T(, the programmer writes a special-purpose C program.) 181.37 576 T5 11 Q0.2 (expect_user) 72 552 P0 12 Q0.2 (,) 131.36 552 P5 11 Q0.2 (send_user) 137.56 552 P0 12 Q0.2 (, and) 188.38 552 P5 11 Q0.2 (send_error) 215.09 552 P0 12 Q0.2 ( \050for writing to the standard error\051 are frequently used in) 268.21 552 P0.02 (longer) 72 538 P2 F0.02 (expect) 105.66 538 P0 F0.02 ( scripts which translate a complex interaction from a process to a simple one for the) 138.3 538 P0.29 (user) 72 524 P0.29 (. In [7], Libes describes how) 91.32 524 P2 F0.29 (adb) 236.27 524 P0 F0.29 ( could be securely wrapped with a script, preventing a sys-) 255.6 524 P0.38 (tem administrator from needing to master the intricacies of) 72 510 P2 F0.38 (adb) 360.85 510 P0 F0.38 (, while at the same time dramati-) 380.19 510 P(cally lessening the likelihood of a system crash due to an errant keystroke.) 72 496 T-0.24 (A simpler example is automating) 72 472 P2 F-0.24 (ftp) 233.03 472 P0 F-0.24 ( to retrieve \336les from a personal account. In this case, a pass-) 247.69 472 P0.18 (word must be supplied. Storing the cleartext password in a \336le should be avoided even if the \336le) 72 458 P-0.19 (permissions are heavily restricted. Supplying passwords as ar) 72 444 P-0.19 (guments to a script is also a security) 367.75 444 P0.04 (risk due to the ability of) 72 430 P2 F0.04 (ps) 190.15 430 P0 F0.04 ( to retrieve them. A solution is to call) 201.48 430 P5 11 Q0.04 (expect_user) 386.74 430 P0 12 Q0.04 ( at the beginning of) 446.58 430 P0.57 (the script for each password that the script must supply later) 72 416 P0.57 (. The password will be available to) 365.48 416 P(the script \050and only to the script\051, even if it has to retry) 72 402 T2 F(ftp) 337.15 402 T0 F( every hour) 351.81 402 T(.) 405.77 402 T0.57 (This technique is useful even if the information is to be entered immediately) 72 378 P0.57 (. For example, you) 444.46 378 P-0.08 (can write a script which changes your password on every machine on which you have an account,) 72 364 P0.44 (whether or not the machines share a common password database \050or even run UNIX\051. By hand,) 72 350 P-0.1 (you might have to) 72 336 P2 F-0.1 (telnet) 161.54 336 P0 F-0.1 ( to each machine and then enter the new password. W) 190.18 336 P-0.1 (ith) 449.71 336 P2 F-0.1 (expect) 465.27 336 P0 F-0.1 (, you en-) 497.9 336 P(ter the password once and let the script do the rest of the work.) 72 322 T5 11 Q0.17 (expect_user) 72 298 P0 12 Q0.17 ( and) 131.84 298 P5 11 Q0.17 (interact) 155.5 298 P0 12 Q0.17 ( can also be mixed in a single script. Imagine debugging a program that) 191.52 298 P1.63 (only fails after many iterations of a loop. An) 72 284 P2 F1.63 (expect) 307.84 284 P0 F1.63 ( script could drive the debugger) 340.48 284 P1.63 (, setting) 500.38 284 P0.2 (breakpoints, running the program for the appropriate number of loops, and then returning control) 72 270 P-0.19 (to the keyboard. It could also alternate between looping and testing for a condition, before return-) 72 256 P(ing control.) 72 242 T3 16 Q(10. Programming expect interactively) 108 201.33 T2 12 Q1.2 (expect) 72 176 P0 F1.2 ( may be programmed interactively) 104.63 176 P1.2 (. For example, if) 273.86 176 P2 F1.2 (expect) 365.46 176 P0 F1.2 ( is run with no ar) 398.1 176 P1.2 (guments, it) 485.49 176 P1.31 (prompts for commands. This is similar to what one normally does when interactively using a) 72 162 P-0.12 (shell. This interactive mode may also be entered by pressing a user) 72 148 P-0.12 (-de\336ned string during) 394.41 148 P5 11 Q-0.12 (interact) 500.98 148 P0 12 Q-0.12 (.) 537 148 P-0.19 (Once prompted by the interpreter) 72 124 P-0.19 (, you can type) 230.63 124 P2 F-0.19 (expect) 300.15 124 P0 F-0.19 ( commands which are executed immediate-) 332.78 124 P-0.07 (ly) 72 110 P-0.07 (. Y) 80.55 110 P-0.07 (ou can call de\336ned procedures, perform job control, or even recursively invoke) 96.88 110 P5 11 Q-0.07 (interact) 478.45 110 P0 12 Q-0.07 (. For) 514.48 110 P0.64 (example, suppose you are running a script to automate) 72 96 P2 F0.64 (fsck) 342.21 96 P0 F0.64 (. Y) 362.86 96 P0.64 (ou answer some of the questions) 380.59 96 P-0.13 (yourself, and then decide that the rest should all be answered \322yes\323. Y) 72 82 P-0.13 (ou can escape from) 408.57 82 P5 11 Q-0.13 (interact) 503.98 82 PFMENDPAGE%%EndPage: "13" 12%%Page: "12" 12612 792 0 FMBEGINPAGE72 36 504 47.95 R7 X0 KV0 12 Q0 X(12) 72 39.95 T0 10 Q(Don Libes) 108 39.95 T72 740.81 504 756 R7 XV72 63 540 720 RV0 12 Q0 X0.81 (a) 72 712 P5 11 Q0.82 (^Z) 81.13 712 P0 12 Q0.81 (, the process stops and control returns to the shell. As far as) 92.99 712 P2 F0.81 (expect) 395.4 712 P0 F0.81 ( is concerned, it is still) 428.03 712 P(dealing with the same process \050the original shell\051.) 72 698 T0.64 (Not only is) 72 674 P2 F0.64 (expect) 130.23 674 P0 F0.64 (\325) 162.87 674 P0.64 (s approach \337exible, it also avoids duplicating the job control software that is) 166.2 674 P0.15 (already in the shell. By using the shell, you get the job control of your choice since you can pick) 72 660 P-0.05 (the shell to spawn. And should you need to \050such as when testing\051, you really can drive a shell so) 72 646 P0.08 (that it thinks it is running interactively) 72 632 P0.08 (. This is also useful for programs that change the way they) 255.6 632 P(buf) 72 618 T(fer output after detecting whether they are running interactively or not.) 87.77 618 T0.56 (T) 72 594 P0.56 (o further pin things down, during) 78.49 594 P5 11 Q0.56 (interact) 244.05 594 P0 12 Q0.56 (,) 280.08 594 P2 F0.56 (expect) 286.63 594 P0 F0.56 ( puts the controlling terminal \050the one) 319.26 594 P2 F0.56 (expect) 507.37 594 P0 F0.53 (was invoked from, not the pty\051 into raw mode so that all characters pass to the spawned process) 72 580 P0.54 (verbatim. When) 72 566 P2 F0.54 (expect) 156.88 566 P0 F0.54 ( is not executing) 189.52 566 P5 11 Q0.55 (interact) 273.62 566 P0 12 Q0.54 (, the terminal is in cooked mode, at which time) 309.64 566 P(shell job control can be used on) 72 552 T2 F(expect) 226.9 552 T0 F( itself.) 259.53 552 T3 16 Q(9. Using expect interactively) 108 511.33 T0 12 Q1.09 (Earlier were shown scripts that are used interactively with) 72 486 P5 11 Q1.11 (interact) 363.24 486 P0 12 Q1.09 (.) 399.27 486 P5 11 Q1.11 (interact) 410.44 486 P0 12 Q1.09 ( essentially gives a) 446.46 486 P-0.07 (user free access to the dialogue, but sometimes \336ner control is desired. This can be achieved us-) 72 472 P-0.12 (ing) 72 458 P5 11 Q-0.12 (expect) 90.21 458 P0 12 Q-0.12 ( which can read from the standard input just as easily as it reads from a process. A pre-) 122.57 458 P3.56 (de\336ned) 72 444 P5 11 Q3.63 (spawn_id) 113.87 444 P0 12 Q3.56 ( maps to the standard input and the standard output. Alternatively) 160.27 444 P3.56 (, the) 515.79 444 P0.06 (commands) 72 430 P5 11 Q0.06 (expect_user) 127.03 430 P0 12 Q0.06 ( and) 186.87 430 P5 11 Q0.06 (send_user) 210.3 430 P0 12 Q0.06 ( perform I/O with the standard input and the standard out-) 261.6 430 P(put without changing) 72 416 T5 11 Q(spawn_id) 176.94 416 T0 12 Q(.) 223.35 416 T0.33 (The following script reads a line from the standard input for a given amount of time. This script) 72 392 P2.87 (\050named) 72 378 P2 F2.87 (timed_r) 113.83 378 P2.87 (ead) 154.25 378 P0 F2.87 (\051 can be called from, for example, a) 172.24 378 P2 F2.87 (csh) 369.01 378 P0 F2.87 ( script as) 385.66 378 P4 F6.88 (set answer =) 439.89 378 P(\324time) 72 364 T(d_read 30\324) 107.98 364 T0 F(.) 179.94 364 T4 F(#!/usr/local/bin/expect -f) 115.2 344 T(set timeout [index $argv 1]) 115.2 330.5 T(expect_user "*\134n") 115.2 317 T(send_user $expect_match) 115.2 303.5 T0 F0.96 (The third line accepts any newline-terminated line from the user) 72 280 P0.96 (. The last line returns it to the) 388.08 280 P(standard output. If nothing is typed before the timeout, nothing is returned.) 72 266 T-0.22 (The \336rst line allows systems that support the) 72 242 P5 11 Q-0.23 (#!) 287.43 242 P0 12 Q-0.22 ( magic to invoke the script directly \050without saying) 296.6 242 P4 F-0.44 (expect) 72 228 P0 F-0.19 ( before the script name\051 if its execute permission is set. Of course a script can always be) 115.18 228 P-0.07 (invoked explicitly) 72 214 P-0.07 (, as) 158.1 214 P4 F-0.17 (\322expect script\323) 176.95 214 P0 F-0.07 (. Options preceded by a) 284.72 214 P5 11 Q-0.07 (\320c) 403.86 214 P0 12 Q-0.07 ( \337ag are executed as com-) 415.47 214 P0.28 (mands before any in the script. For example, an) 72 200 P2 F0.28 (expect) 309.25 200 P0 F0.28 ( script can be traced without reediting by) 341.88 200 P0.59 (invoking it as) 72 186 P4 F1.42 (expect -c "trace ..." script) 142.08 186 P0 F0.59 ( \050where the ellipsis represents a tracing) 349.26 186 P(option\051.) 72 172 T0.42 (Multiple commands may be strung together on a single script line or within braces, separated by) 72 148 P0.79 (semi-colons. Naturally) 72 134 P0.79 (, this extends to the) 185.05 134 P5 11 Q0.8 (\320c) 284.93 134 P0 12 Q0.79 ( ar) 296.53 134 P0.79 (gument. For example, the following command) 309.42 134 P(runs program) 72 120 T2 F(foo) 139.28 120 T0 F( for 20 seconds.) 155.27 120 T4 F(expect -c "set timeout 20; spawn foo; expect") 115.2 100 TFMENDPAGE%%EndPage: "12" 11%%Page: "11" 11612 792 0 FMBEGINPAGE72 740.81 540 756 R7 X0 KV72 36 540 47.95 RV0 10 Q0 X(expect: Scripts for Controlling Interactive Processes) 295.54 39.95 T0 12 Q(11) 528.01 39.95 T72 63 540 720 R7 XV4 F0 X(case $tty in $ready {) 162 712 T(set spawn_id $tty) 207 698.5 T(expect "*\134r*" {) 207 685 T(if $log {) 297 671.5 T(send_user $expect_match) 342 658 T(set log 0) 342 644.5 T(}) 297 631 T(} "*") 207 617.5 T(if $log {) 207 604 T(send_user $expect_match) 252 590.5 T(}) 207 577 T(set spawn_id $login; send $expect_match) 207 563.5 T(}) 162 550 T(}) 115.2 536.5 T0 F0.28 (The script works as follows. First connections are made to a) 72 513 P2 F0.28 (login) 369.45 513 P0 F0.28 ( process and terminal. By de-) 394.78 513 P-0.02 (fault, an entire session is logged to the standard output \050via) 72 499 P5 11 Q-0.02 (send_user) 357.59 499 P0 12 Q-0.02 (\051. Since this is not of inter-) 408.89 499 P1.61 (est, it is disabled by the command) 72 485 P4 F3.86 (log_user 0) 249.15 485 P0 F1.61 (. \050A variety of commands are available to) 324.97 485 P(control exactly what is seen or logged.\051) 72 471 T1.05 (In a loop,) 72 447 P5 11 Q1.07 (select) 123.79 447 P0 10 Q0.88 (3) 152.49 451.8 P0 12 Q1.05 ( waits for activity from either the terminal or the process and returns a list of) 157.48 447 P5 11 Q0 (spawn_id) 72 43
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -