title: Bash script - for loop and if-else
body: <p>I am new to Bash scripting and despite multiple attempts to refactor the logic structure shown in pseudocode below I cant get this to work.  How do I piece the if/else logic together with a for loop?</p>
  
  <p><strong>Pseudocode</strong>:
   I have a command that checks if any subfolders exist in a directory of the HDFS filesystem.  Lets call this command_A.</p>
  
  <pre><code>If subfolders do NOT exist {
      proceed with remaining execution of script
  }
  Else {
      sleep for 30 minutes and run command_A again to see if the subfolders have been removed.  Note:  This sleep and re-check cycle should repeat up to 4 times and if subfolders are never removed the script is killed with exit 1
  }
  </code></pre>
  
  <p>Sample of What I Have Tried is Below.  I can't figure out how I am supposed to use the || in conjunction with the else statement.</p>
  
  <p><strong>Using these structures</strong>:</p>
  
  <pre><code>1. for i in {1..4}; do command_A &amp;&amp; break || sleep 1800; done
  2. if command_A ; then echo "Command succeeded" else echo "Command failed" fi 
  </code></pre>
  
  <p><strong>Test Example</strong>:    </p>
  
  <pre><code>for i in {1..4};
    do
       if hdfs dfs -test -e $mypath/*
         echo "Folder is empty" &amp;&amp; break
  else
       ???
  </code></pre>
  
  <p><strong><em>Update showing working solution</em></strong>:</p>
  
  <pre><code>for i in {1..4};
    do
     if hdfs dfs -test -e $mypath/*;
       then
         if [ $i -eq 4 ]
          then
            echo "script exiting now with code 1"
         else
            echo "Folder is full"
            sleep 10
         fi
     else
      echo "Folder is empty"
      break
    fi
  done
  </code></pre>