title: Wait Until Previous Command Completes
body: <p>I have written a bash script on my MacMini to execute anytime a file has completed downloading.  After the file download is complete, the mac mounts my NAS, renames the file, and then copies the file from the mac to the NAS, deletes the file from the mac and then unmounts the NAS.</p>
  <p>My issue is, sometimes, the NAS takes a few seconds to mount.  When that happens, I receive an error that the file could not be copies because the directory doesn’t exist.</p>
  <p>When the NAS mounts instantly, (if the file size is small), the file copies and then the file deletes and the NAS unmounts.</p>
  <p>When the file size is large, the copying process stops when the file is deleted.</p>
  <p>What I’m looking for is, how do I make the script “wait” until the NAS is mounted, and then how do I make the script again wait until the file copying is complete?</p>
  <p>Thank you for any input. Below is my code.</p>
  <pre class="lang-sh prettyprint-override"><code>#connect to NAS
  open 'smb://username:password@ip_address_of_NAS/folder_for_files'
  
  #I WANT A WAIT COMMAND HERE SO THAT SCRIPT DOES NOT CONTINUE UNTIL NAS IS MOUNTED
  
  #move to folder where files are downloaded
  cd /Users/account/Downloads
  
  #rename files and copy to server
  for f in *; do
      newName=&quot;${f/)*/)}&quot;;
      mv &quot;$f&quot;/*.zip &quot;$newName&quot;.zip;
      cp &quot;$newName&quot;.zip /Volumes/folder_for_files/&quot;$newName&quot;.zip;
      
      #I NEED SCRIPT TO WAIT HERE UNTIL FILE HAS COMPLETED ITS TRANSFER
  
      rm -r &quot;$f&quot;;
  done
  
  #unmount drive
  diskutil unmount /Volumes/folder_for_files
  </code></pre>