title: Modifying a shell variable in a while loop does not work as expected
body: <p>This is a very simple bash script I wrote:</p>
  
  <pre><code>#!/bin/bash
  
  ITEM_LIST=items.txt
  LOG_FILE=log.log
  
  TOTAL_ITEMS=$(wc -l ${ITEM_LIST} | awk '{ print $1 }')
  let NOT_FOUND=0
  
  cat ${ITEM_LIST} | while read item; do
  
      grep "${item}" ${LOG_FILE} &gt; /dev/null
      FOUND=${?}
      if [ ${FOUND} -ne 0 ]; then
          let NOT_FOUND=NOT_FOUND+1
          echo "Item not found [${item}] Item not found number: ${NOT_FOUND}"
      fi
  
  done
  
  echo "Total items: ${TOTAL_ITEMS}"
  echo "Total not found items: ${NOT_FOUND}"
  </code></pre>
  
  <p>I want to check if some items are present in a log file, count how many are not present, and print some kind of reporting (last two echo). At this moment, I'm running it from cygwin bash shell.</p>
  
  <p>Consider these two sample files:</p>
  
  <p>items.txt</p>
  
  <pre><code>first item
  second item
  third item
  fourth item
  fifth item
  </code></pre>
  
  <p>log.log</p>
  
  <pre><code>blahblah blah blah first item blah blah blah
  second blah blah item
  blah third item blah
  </code></pre>
  
  <p>Script's output:</p>
  
  <pre><code>[17:46:38]:/cygdrive/c/Temp/qpa# ./script2.sh 
  Item not found [second item] Item not found number: 1
  Item not found [fourth item] Item not found number: 2
  Total items: 4
  Total not found items: 0
  </code></pre>
  
  <p>Question:</p>
  
  <p>Why is the script printing "Total not found items: 0"? It prints the current total on every echo on the loop (both NOT_FOUND variable).</p>
  
  <p>Are there some bad practices in this shell script? Where and why?</p>