This article explains the command line options that can be passed to an expect script.
If you are new to expect scripting language, first start with our expect hello world example.
1. Execute expect script from the command line using -c option
expect also also allows you to execute it directly in the command line using -c option as shown below.
$ expect -c 'expect "\n" {send "pressed enter\n"} pressed enter $
Once you run the above script, it waits for the newline(\n) and upon pressing the enter key, it will print the message “pressed enter” and exits.
2. Execute expect script interactively using -i option
Expect script can run interactively by reading commands from the standard input using -i option as shown below.
$ expect -i arg1 arg2 arg3 expect1.1>set argv arg1 arg2 arg3 expect1.2>
Normally when you run the above expect command without -i option, it would treat the arg1 as script filename, so the -i option makes the argument list as uninterrupted.
This option is useful when you run the expect script with the -c flag. Because by default expect runs interactively.
3. Print debug messages while executing expect script
You might enable the diagnostics messages to be printed when you run the code with -d option as shown below.
$ cat sample.exp # !/usr/bin/expect -f expect "\n"; send "pressed enter"; $ expect -d sample.exp expect version 5.43.0 argv[0] = expect argv[1] = -d argv[2] = sample.exp set argc 0 set argv0 "sample.exp" set argv "" executing commands from command file sample.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. Enable expect debugger using -D
-D option is used to enable the debugger and it just takes the boolean value as an argument. This will indicate whether the debugger has to be started or just initialize it and use it at the later time.
$ expect -D 1 script
The options before the left of the -D option will get processed before the debugger. Then the remaining commands will get executed after starting the debugger.
$ expect -c 'set timeout 10' -D 1 -c 'set a 1' 1: set a 1 dbg1.0>
5. Execute expect script line by line
Normally expect reads the entire script into the memory before executing it. -b option makes the expect to read the script one line at a time. This could be useful when you have not written the completely by that time and expect starts executing it and thus it avoids writing of temporary file.
$ expect -b
6. Make Command Line Arguments not to be Interpreted
You can make the expect not interpret the command line arguments using — flag.
Normally you would read the command line arguments as shown below.
$ cat print_cmdline_args.exp #!/usr/bin/expect puts 'argv0 : [lindex $argv 0]'; puts 'argv1 : [lindex $argv 1]';
While executing the above script, pass the command line options, which will be treated like an argument (instead of expect options) as shown below.
$ expect print_cmdline_args.exp -d -c argv0 : -d argv1 : -c
Comments on this entry are closed.
There’s a typo in your first script. It’s missing the closing quote. Should be:
expect -c ‘expect “\n” {send “pressed enter\n”}’
instead of:
expect -c ‘expect “\n” {send “pressed enter\n”}
Please help me “””” how press enter key in expect scripting please anyone “””
Sudershan – “\r” is carriage return or pressing enter key.