An example wherein Expect controls a pager

Summary

A common question is how to get Expect to control a file pager such as more or less to automatically page through the entire contents, without having to manually step through each page of text. One use case for this is to page through a license file when installing some software.

Description

First, check the pager being used to make sure it doesn't have a "show everything" mode. If it does, use that instead of Expect.

As a model of the value of exp_continue, though, this problem is valuable. An example solution follows.

[Explain eof vs. a remote prompt, ...]

package require Expect

set more_prompt "--More-- or (q)uit"
set more_prompt %)
set shell_prompt {$ }
set go_ahead_reply " "
set example_textfile /etc/passwd

log_user 0

spawn more $example_textfile

expect -- $more_prompt {
    send $go_ahead_reply
    puts "The current display is in exp_out(buffer)."
    exp_continue
} $shell_prompt {
    puts "This is a branch for a remote process."
} eof {
    puts "That's the end."
} timeout {
    puts "This is strange."
}

Here's a more reduced example of the same ideas, one which takes advantage of the expect command's other pattern-action syntax:

set m "--More--"
expect -- {
    $m {send " ";exp_continue}
    "# " {# Done!}
}