title: How to pass awk variables to a bash loop
body: <p>I'm processing a csv file in awk. During the script, I need to exit awk for a little while, go and do some ImageMagick processing in the bash shell based on the information awk processed in the csv file. I'm trying to do this image processing in the shell because I loop through all the jpg files in the nominated directory. My question is twofold:</p>
  
  <ol>
  <li>How can I pass variables from awk to bash so that I can do this? (this is <code>$imageDirectory</code> and <code>$productRef</code> in the code below)</li>
  <li>I'm trying to avoid lots of <code>system(someImageMagicCommand)</code> type of code, because it looks like I'm trying to use awk to do something it's not designed to do. Is there a better approach?</li>
  </ol>
  
  <p>Here's the pseudocode sample that illustrates what I'm trying to achieve:</p>
  
  <pre><code>    #blah blah blah awk code
      #leave awk interpreter, go into bash
      '
      resizesize="200x200";
      #concatenate with *.jpg suffix for listing all the jpg files in the imageDirectory
      imageDirectoryWithSuffix="$imageDirectory/*.jpg";
      for i in `ls $imageDirectoryWithSuffix`
      do
              #imageMagick converts large images to thumbnails
              convert $i $resizeSize -otherFlagsEtc assets/$productRef/thumbs/$i
      done
      '#back into awk, more csv processing now...
  </code></pre>
  
  <p><strong>Clarifying context:</strong> I have a csv file full of product information and associated file paths (1 product per line). I'm trying to automate the creation of webpages about the products. Part of this involves resizing a directory of images, the location of which is given as a field in the csv file. Hence, part way through the script, I'm trying to leave the awk interpreter, invoke ImageMagick to resize, and then return to the awk interpreter <strong><em>at the same record in the csv file</em></strong> and keep outputting HTML files.</p>
  
  <p>There's about 100 (long) lines of script code before I get to the ImageMagick part and another hundred afterwards, so based on @Bushmills answer I think the best thing would be to write the awk variables I'll need in bash to a small temp file, then exit awk and read in the temp file from bash. However, how do I reinvoke awk and get it to start reading at the same record where it left off? Or do I just have to stay in awk and use a <code>system()</code> call? It doesn't seem sensible to wrap an entire bash <code>for</code> loop in an awk <code>system()</code> function, but I can't think of a more elegant way of calling ImageMagick on an entire directory of files.</p>