Home > OS >  Under Linux flexible use of expect script tips
Under Linux flexible use of expect script tips

Time:09-17

Under Linux, flexible use of expect script tips


For love automated Linux system administrators, expect the command line tools must be used, expect is by Don Libes based on Tcl language development, and is widely used in interactive operation and test automation scenes, it is especially applicable to the need for multiple servers performing the same operation environment, can greatly improve the efficiency of system management personnel, this article is thegeekstuff.com recently updated an article technology sharing, which has been given details of how to use different command line options to perform a expect scripts, what's the use of specific, you can free imagination,

This article assumes that you expect already have a certain understanding, the basic way of using the

If you expect script language, novice can expect from us first "hello world" sample (in English),

1. Use "-c" option from the command line to perform expect script

Expect allows you to use "-c" option, directly on the command line to execute it, as shown in the following:

$expect - c "expect" \ n "{send" pressed the enter \ n "}

Pressed the enter

$

If you execute the above script, it will wait for input a newline character (\ n), press "enter" key, it will print out the message "pressed the enter", then quit,

2. Use "-i" option interactively performs expect script

Use the "-i" option, can be read from standard input command to interactively performs expect scripts, as shown in the following:

$expect -i arg1 arg2 arg3

Expect1.1 "set argv

Arg1 arg2 arg3

Expect1.2
"
Under normal circumstances, when you follow the above expect orders (no "-i" option), it will take arg1 as script file name, so "-i" option allows scripts using multiple parameters as a continuous list,

When you execute expect scripts with "-c" option, this option is useful, because, by default, expect is interactively,

3. Perform expect scripts, output debugging information

When you use "-d" option when executing code, you can output the diagnosis information, as shown in the following:

$cat sample. J exp

#! The/usr/bin/expect - fexpect "\ n"; Send "pressed the enter"; $expect - d sample. Expexpect version 5.43.0 argv [0]=expect argv [1]=-d argv [2]=sample. Expset arg c 0 set argv0 "sample. J exp" set argv "" executing commands from the command file sample. J exp

Expect: does "" (spawn_id exp0) match glob pattern" \ n "? No

Expect: does "\ n" (spawn_id exp0) match glob pattern "\ n"? Yes

Expect: set expect_out (0, string)
"\ n"
Expect: set expect_out (spawn_id) "exp0
"
Expect: set expect_out (buffer)
"\ n"
Send: sending pressed "enter" to {exp0 pressed enter}

4. Use the "-d" option to start the expect debugger

"-d" option is used to start the debugger, it only accepts a Boolean parameter, this parameter said cuing component must be started immediately, or just initializes the debugger, then use it,

1 $expect - D script

"-d" option on the left side of the option will be processed before debugger launch, and then, in the debugger starts, the rest of the command will be executed,

$expect - c 'set the timeout 10' 1 - c - D set a '1'

1: set a 1

Dbg1.0
"
5. Line to perform expect script

Often, expect before executing scripts, loaded into memory, the whole script "- b" option allows expect a row in a read only the script, when you didn't write a complete script, it is very useful, expect can begin to carry out the incomplete script, and it can avoid the script written to a temporary file,

$expect - b

6. Don't expect to explain command line parameter

You can use the identifier to expect not explain command line parameters,

You can read the command line parameters such as the following:

$cat print_cmdline_args. J exp

#! The/usr/bin/expect

Puts' argv0: [lindex $argv 0] ';

Puts' argv1: [1] lindex $argv ';

When performing the above script, will skip command line options, they will be as a parameter (not expect option), as shown in the following:

$expect print_cmdline_args. J exp - d - c

Argv0: -d

Argv1: - c
  • Related