title: Expect: Same pattern, different answer
body: <p>I'm trying to automate the answers on a menu-like script to Start/Stop a propietary service. </p>
  
  <p>At first, the menu asks which type of service are you trying to affect <em>(press 1)</em>. Then, asks you for the action to perform <em>(press 2 to stop)</em>. Then, performs the action and tells you to press any key to continue, which takes you back to the "action" menu, where you have to press 0 to go back to the first menu, then 0 again to quit.</p>
  
  <p>The thing is, if I just ignore the steps after the service is stopped, next time I enter the menu script, it skips the first part and goes straight to the "action" menu (It saves the option you selected on the first menu somewhere, but I don't know where).</p>
  
  <p>Is there any way to tell expect to <code>send "0"</code> the second time it sees the "action" menu?</p>
  
  <p>This is the script I have right now:</p>
  
  <pre><code>#!/usr/bin/expect
  set startstop [lindex $argv 0]
  
  if {$startstop == "start"} {
    set sendnumber "1"
  } elseif {$startstop == "stop"} {
    set sendnumber "2"
  } else {
    exit 0
  }
  
  log_user 0
  spawn ./menuscript.sh
  expect {
    "first menu" {
      send "1"
      exp_continue
    }
    "action menu" {
      send "$sendnumber"
      exp_continue
    }
    "Press any key to continue" {
      send "\r"
      exit 1
    }
  }
  </code></pre>