title: How To Check When Curl PID is Done from Bash Loop
body: <p>I can't figure out my bug on OSX. When I try to see when Curl is finished, the process remains loaded. I never see the CURL FINISHED message.</p>
  
  <pre><code>#!/bin/bash
  
  curl -S -o example.com http://example.com/downloads/example.zip &amp;
  CURL_PID=$!
  echo -e "CURL PID = $CURL_PID"
  while :
  do
      sleep 1
      if [ -n $(ps -p$CURL_PID -o pid=) ]; then
          echo "CURL NOT FINISHED"
      else
        echo "CURL FINISHED"
        break
      fi
  done
  </code></pre>
  
  <p>Note on OSX's version of Bash when I run this:</p>
  
  <pre><code>#!/bin/bash
  
  PIDX=1
  if [ -n $(ps -p$PIDX -o pid=) ]; then
    echo "PROCESS 1 IS THERE"
  else
    echo "PROCESS 1 IS NOT THERE"
  fi
  </code></pre>
  
  <p>...it says Process 1 is there. (Everyone has a PID 1, so this is just an example.) So, I know that my <code>if</code> statement is correct. No double quotes necessary on the <code>if</code> line.</p>
  
  <p>Note that I can't use wait on the <code>$CURL_PID</code> because what you don't see here is that I also am using OSX's <code>osascript</code> command to show a dialog that says "Downloading...", which also has a Cancel button on it and its own <code>$DLG_PID</code>, and so I'm looping endlessly until either they cancel the dialog (meaning <code>$DLG_PID</code> points is gone) or <code>$CURL_PID</code> is gone (meaning the download finally completed so I can run <code>kill $DLG_PID</code> now).</p>
  
  <p>On OSX, note I'm doing this as well before the <code>curl</code> statement.</p>
  
  <pre><code>osascript -e 'tell app "System Events" to display dialog "Downloading..." with title "My App Installer" buttons {"Cancel"}' &amp;
  </code></pre>
  
  <p>So, if someone cancels the dialog, I kill the curl by PID and exit the infinite loop (and exit the bash script). If they don't cancel that dialog, and the curl finishes, then I kill the dialog by PID and exit the bash script.</p>