6 Expect Script Command Line Argument Examples

by Balakrishnan Mariyappan on December 21, 2010

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

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 2 comments… read them below or add one }

1 Kevin March 16, 2013 at 4:44 am

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”}

2 Sudershan March 27, 2013 at 8:03 am

Please help me “”"” how press enter key in expect scripting please anyone “”"

Leave a Comment

Previous post:

Next post: