title: How to create a stack in a shell script?
body: <p>I need to create a stack in a shell script in order to push values to be processed in a loop. The first requirement is that this must be implemented in a portable manner, as I want to use the script as a portable installer (at least between Unix-like operating systems). The second requirement is that it needs to be able to be altered inside the loop, because new information can appear while the loop is processing an entry, in a recursive manner. The third requirement is that I have more than one line of information per entry (this is mostly a fixed number, and when it isn't it can be calculated based on the first line of information).</p>
  
  <p>My attempt is to use a stack file:</p>
  
  <pre><code>#!/bin/sh
  
  echo "First entry" &gt; stack.txt
  echo "More info for the first entry" &gt;&gt; stack.txt
  echo "Even more info for the first entry" &gt;&gt; stack.txt
  
  while read ENTRY &lt; stack.txt; do
      INFO2=`tail -n +2 stack.txt | head -n 1`
      INFO3=`tail -n +3 stack.txt | head -n 1`
  
      tail -n "+4" stack.txt &gt; stack2.txt
  
      # Process the entry...
  
      # When we have to push something:
      echo "New entry" &gt; stack.txt
      echo "Info 2" &gt;&gt; stack.txt
      echo "Info 3" &gt;&gt; stack.txt
  
      # Finally, rebuild stack
      cat stack2.txt &gt;&gt; stack.txt
  done
  </code></pre>
  
  <p>This works perfectly, except that it feels wrong. Is there a less "hacky" way to do this?</p>
  
  <p>Thanks in advance for any help!</p>