Send multiple response to interactive Linux command using expect
<p>I have below task to perform. </p>

<ol>
<li>Run a  linux command 'abc' and it will ask for multiple response</li>
<li>First, there are multiple lines and it is asking to select option from 1-10. Ending with 'run?', always have to select 1</li>
<li>Second, Ending with 'yes/no'. Always 'yes' response</li>
<li>Third, Enter ID. Take one ID as input from a .txt file. There is one ID in each row. </li>
<li>Fourth, y/n. Always select "y" as response.</li>
</ol>

<p>Step 2-5 should run in loop till all ID's in .txt file get over and step 5 will select 'no' or just exit.</p>

<blockquote>
  <p>Tried below code in Shell/expect but sometimes it skip the ID's from
  list or show blank value and sometimes get crash while running and
  throw error:</p>
</blockquote>

<pre><code>*child process exited abnormally
    while executing
"exec cat output.txt | grep -i -B2  "rows selected" &gt; result.txt"
    (file "./cmp-test.sh" line 31)*
</code></pre>

<p><strong>Here is the code:</strong> </p>

<pre><code>exec echo "" &gt; output.txt  
log_file [pwd]/output.txt
set f [open list.txt r]
# list is the file name contain ID's

set idlist [ split [ read $f ] "\n" ]
close $f

send_user "\n Running script.. \n"

spawn &lt;abc command&gt;
foreach ids $idlist {
expect {
  "run? " { send "1\r" }
}

expect {
  "ACDIG: " { send  "$ids\r" }
}

expect { 
  "n)?"  { send "y\r" } 
}

}

exec cat output.txt | grep -i -B2  "rows selected" &gt; result.txt
</code></pre>