title: How to use expect with optional prompts?
body: <p>Let's say I am trying to write an expect script for a test.sh that has three prompts: prompt1, prompt2, prompt3.</p>
  
  <p>My code is like this:</p>
  
  <pre class="lang-sh prettyprint-override"><code>spawn test.sh
  expect "prompt1"
  send "pass1"
  expect "prompt2"
  send "pass2"
  expect "prompt3"
  send "pass3"
  </code></pre>
  
  <p>However, prompt2 only occurs half the time. If prompt2 doesn't show up, the expect script breaks. How would I write expect code that skips over prompt2 if it doesn't show up?</p>
  
  <p>EDIT: </p>
  
  <p>Fixed my code:</p>
  
  <pre class="lang-sh prettyprint-override"><code>/usr/bin/expect -c '
  spawn ./test.sh
  expect {
        "prompt1" {
            send "pass1\r"
            exp_continue
        }
        "prompt2" {
            send "pass2\r"
            exp_continue
        }
        "prompt3" {
            send "pass3\r"
            exp_continue
        }
  }
  interact return
  '
  </code></pre>
  
  <p>This way, the rest of the script executes and provides output.</p>