Version 12 of An example wherein Expect controls a pager

Updated 2006-07-18 18:53:08

Requests often arrive for the magic needed to get Expect to control more(1) or a similar pager so as to see a "whole report" automatically, and not have to step through the screens the pager affords. These pleas are often mistaken, in the sense that most report generators and/or pagers have what amounts to a show-it-all mode, and so don't require Expect's blessings.

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

[Explain context, how it illustrates use of exp_continue, how pagers themselves usually afford enough programmatic control that Expect is overkill, how "pager" hear means something like "more" or "less", not http://en.wikipedia.org/wiki/Pager , ...] [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:

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