title: Leaving a while loop to execute next command in BASH script in a linux environment
body: <p>I hope you are all doing well! I am trying to poll a directory once every so often until data is done entering the directory, and when it finishes I want to execute my next task on the directory using a bash script in a linux environment. I do not have access to inotify-tools. The way I am testing this process is by typing the following into one shell command line:</p>
  
  <pre><code>while true; do touch /home/tmp/fun_temp/nothing.txt; sleep 10; rm -rf /home/tmp/fun_temp/nothing.txt; done
  </code></pre>
  
  <p>This creates a file in a temp directory called nothing.txt and then sleeps 10 seconds and then removes it, then does it again until I tell it to stop. My bash script watches the directory at intervals of 20 seconds and then at 20 seconds writes the directory info into a file called old_stat.txt. Then after another 20 seconds it compares a new_stat with the old stat, and if they are equal it should exit the loop and do the next part of the command, and if they are not equal to keep polling. Here is my bash script:</p>
  
  <pre><code>#!/bin/bash
  DIR_TO_CHECK='/home/tmp/fun_temp/'
  OLD_STAT_FILE='/home/sleep_files/old_stat.txt'
  
  if [ -e $OLD_STAT_FILE ]; then
      OLD_STAT=`cat $OLD_STAT_FILE`
  else
      OLD_STAT="nothing"
  fi
  
  NEW_STAT=`stat -t $DIR_TO_CHECK`
  
  while true; do
      if [ "$OLD_STAT" != "$NEW_STAT" ]; then
          echo 'Directory is still updating.'
          sleep 20
          echo $NEW_STAT &gt; $OLD_STAT_FILE
          echo 'Checking again.'
      elif [ "$OLD_STAT" == "$NEW_STAT" ]; then
          echo 'Directory is done updating. Move on.'
          exit 0
      fi
  done
  </code></pre>
  
  <p>I am using exit 0 as a check at the moment. I test the bash script by running the while loop above in the command line and then in another shell I run this bash script, then I give it about a minute to make sure the directory has changed, then I kill the while loop in the command line and let the bash script run another minute but it never changes to the second condition and leaves the loop unless I kill the script and run it again. Any help would be great, thanks!</p>