title: Count files and directories using shell script
body: <p>I'm learning bash scripting and have written a script to count the files and directories in the directory that is supplied as argument. I have it working one way which seems odd to me and am wondering if there is a simpler way of doing it.</p>
  
  <p>I have commented out the code that will work, but left it in as a comparison. I am trying to get the <code>for</code>-loop working, instead using <code>if</code> statements inside it to detect if an item in the given location is a file or a directory.</p>
  
  <p><strong>Edit</strong>: I just found out that the commented code counts all files and directories in the subdirectories of the given location as well! Is there any way to prevent this and just count the files and directories of the given location?</p>
  
  <pre><code>#!/bin/bash
  
  LOCATION=$1
  FILECOUNT=0
  DIRCOUNT=0
  
  if [ "$#" -lt "1" ]
  then
      echo "Usage: ./test2.sh &lt;directory&gt;"
      exit 0
  fi
  
  #DIRS=$(find $LOCATION -type d)
  #FILES=$(find $LOCATION -type f)
  
  #for d in $DIRS
  #do
  #   DIRCOUNT=$[$DIRCOUNT+1]
  #done
  
  #for f in $FILES
  #do
  #   FILECOUNT=$[$FILECOUNT+1]
  #done
  
  for item in $LOCATION
  do
  if [ -f "$item" ]
      then
           FILECOUNT=$[$FILECOUNT+1]
      elif [ -d "$item" ]
          then
           DIRCOUNT=$[$DIRCOUNT+1]
  fi
  done
  
  echo "File count: " $FILECOUNT
  echo "Directory count: " $DIRCOUNT
  </code></pre>
  
  <p>For some reason the output of the <code>for</code>-loop, no matter where I point the location to, always returns:</p>
  
  <pre><code>File count: 0 , Directory count: 1
  </code></pre>