title: Need to explain behavior of read and named pipe
body: <p>I've been working on project for college, in it I have some small scripts that perform operations on a simple database (such as select information), a server script that takes requests from various instances of a client script through a pipe named server.pipe and returns their result (usually a few lines of a text file) through the individual client's named pipe.</p>
  
  <p>For multi line responses, I tried using a while loop in the client to keep reading from the pipe, I used something like this:</p>
  
  <pre><code>read response &lt; $id.pipe
  while [ $response != "end_result" ]; do
      echo $response
      read response &lt; $id.pipe
  </code></pre>
  
  <p>This would sometimes return the full result, other times it would return part of it and other times none. When it didn't return the full result I think the running script was blocked by the pipe as debug statements showed me it didn't execute its final lines.</p>
  
  <p>I fixed it with this instead</p>
  
  <pre><code>tail &lt;$return_pipe &amp;
  </code></pre>
  
  <p>I know that tail will read from a pipe even if its closed but I need help explaining the conditions causing the first one to fail as I'm writing a report on the assignment now. I've done a lot of reading and I sort of get it but I need help with the specifics.</p>
  
  <p>If anyone's wondering, the server script is calling the script like this:</p>
  
  <pre><code>return_pipe=${ar[4]}.pipe
  ./select.sh ${ar[1]} ${ar[2]} ${ar[3]} &gt;$return_pipe &amp;
  </code></pre>
  
  <p>And the part of the select script returning the result is this</p>
  
  <pre><code>echo "start_result"
  cut -d' ' -f$3 ./$1/$2
  echo "end_result"
  </code></pre>
  
  <p>If anyone can help me understand this and explain it that'd be really appreciated. This is my first post so I hope I've formatted it appropriately too! Thanks</p>