title: Expect in Bash script: expect_out not printing out buffer
body: <p>I am trying to capture the output of the "dir" command by logging into a switch, but I am unable to do so. I am using Expect within Bash. I am making use of expect_out to capture output of that command in a buffer and print it out. Actually I want to capture the output and perform some operations on it.</p>
  
  <p>Script:</p>
  
  <pre class="lang-none prettyprint-override"><code>#!/bin/bash
  expect -c "
  spawn telnet 1.1.1.1 2000
  sleep 1
  send \"\r\"
  send \"\r\"
  expect {
  Prompt&gt; { send \"dir\r\"  }
  }
  set output $expect_out(buffer)
  "
  echo "$output"
  </code></pre>
  
  <p>Output:</p>
  
  <pre class="lang-none prettyprint-override"><code>spawn telnet 1.1.1.1 2000
  Trying 1.1.1.1...
  Connected to 1.1.1.1 (1.1.1.1).
  Escape character is '^]'.
  
  
  
  
  Prompt&gt;
  
  Prompt&gt;
  </code></pre>
  
  <p>After these prompts are displayed, the scripts just exits. How can I fix this problem?</p>
  
  <p>After I split it, I can use parameter substitution as well as single quotes. Now I am facing different error.</p>
  
  <p>Script:</p>
  
  <pre class="lang-none prettyprint-override"><code>expect -c "
  spawn telnet $IP $PORT1
  sleep 1
  send \"\r\"
  send \"\r\"
  "
  expect -c '
  expect {
  Prompt&gt; { send \"dir\r\" }
  set output $expect_out(buffer)
  puts "$output"
  }
  '
  </code></pre>
  
  <p>Output:</p>
  
  <pre class="lang-none prettyprint-override"><code>spawn telnet 172.23.149.139 2033
  can't read "expect_out(buffer)": no such variable
  while executing
  "expect {
  Prompt&gt; { send \"dir\r\" }
  set output $expect_out(buffer)
  puts "$output"
  }
  "
  </code></pre>
  
  <hr/>
  
  <p>I changed it to according to the suggestions. But I am still facing errors.</p>
  
  <p>Script:</p>
  
  <pre class="lang-none prettyprint-override"><code>output=$(expect -c '
  spawn telnet '"$IP $PORT1"'
  sleep 1
  send '"\r"'
  send '"\r"'
  
  expect Prompt&gt; { send '"dir\r"'  }
  expect '"\n"'
  expect -indices Prompt&gt;
  puts '"[string range $expect_out(buffer) 0 [expr $expect_out(0,end) - 1]]"'
  
  ')
  
  echo "======="
  echo "$output"
  echo "======="
  </code></pre>
  
  <p>Output:</p>
  
  <pre class="lang-none prettyprint-override"><code>syntax error in expression "(0,end) - 1"
      while executing
  "expr (0,end) - 1"
      invoked from within
  "string range (buffer) 0 [expr (0,end) - 1]"
      invoked from within
  "puts [string range (buffer) 0 [expr (0,end) - 1]]"
  
  =======
  spawn telnet 1.1.1.1 2000
  Trying 1.1.1.1...
  Connected to 1.1.1.1 (1.1.1.1).
  Escape character is '^]'.
  
  
  
  
  Prompt&gt;
  
  Prompt&gt;
  </code></pre>
  
  <pre><code>=======
  </code></pre>
  
  <p>Hence to circumvent the error, I changed, the line</p>
  
  <pre><code>puts '"[string range $expect_out(buffer) 0 [expr $expect_out(0,end) - 1]]"'
  </code></pre>
  
  <p>to</p>
  
  <pre><code>puts '"$expect_out(buffer)"'
  </code></pre>
  
  <p>But then I am getting no error, but the output of <code>dir</code> is also not getting printed. Something like:</p>
  
  <pre><code>Prompt&gt;
  
  Prompt&gt; (buffer)
  </code></pre>