title: BASH if directory contains files or doesn't
body: <p>I created a script that executes some commands based on a condition. If directory contains files then run "screen -r" anything else run "screen". Problem is screen sometimes gets executed even when the directory contains files. </p>
  
  <pre><code>if [ "$(ls -A $DIR)" ]; then
  screen -r
  else
  screen
  fi
  </code></pre>
  
  <p>What I want to do is refine it and break it down into two statements. If directory contains files then run screen-r" &amp; if a directory doesn't contain files run "screen" </p>
  
  <pre><code>if [ "$(ls -A $DIR)" ]; then
  screen -r
  fi
  </code></pre>
  
  <p>&amp;</p>
  
  <pre><code>if ["$(directory without files)"] ; then
  screen
  fi
  </code></pre>
  
  <p>Maybe even a statement that executes based on # of file. If directory contains X amount of files.</p>
  
  <p>Can somebody help me out with this? I hope I explained what I want thoroughly. </p>
  
  <p>Thanks, </p>
  
  <p>Geofferey</p>
  
  <p>Again thank you for all your help, I got it all working now. Here is the final script. It's for the iPhone and an app I'm making called MobileTerm Backgrounder. </p>
  
  <pre><code>#Sets up terminal environment? 
  
  if [[ $TERM = network || -z $TERM ]]; then
  export TERM=linux
  fi
  
  # This script automatically runs screen &amp; screen -r (resume) based on a set of conditions. 
  
  # Variable DIR (variable could be anything)
  
  DIR="/tmp/screens/S-mobile"
  
  # if /tmp/screens/S-mobile list files then run screen -x
  
  if [ "$(ls -A $DIR)" ]; then
  screen -x
  fi
  
  #if /tmp/screens/S-mobile contains X amount of files = to 0 then run screen -q
  
  if [ $(ls -A "$DIR" | wc -l) -eq 0 ]; then
  screen -q
  fi
  </code></pre>