for loop troubles

What I have here is a pretty textbook recursive function. Its purpose right now is simply to display all folders that don't contain folders.
It works fine in all instances I can think of... except one. If there is a folder with a space in its name, the thing goes Kablooie.
AFAIK the problem comes from the fact that the for loop only takes in words of a list, all the text up to the first space, then second space... etc.

I'm really at a loss as to how to cope with this.
Suggestions?

#!/bin/bash

recurser()
{
temp=$1
fix=${temp%/*}

x=`ls -1d "$fix"/*/ 2> /dev/null`

if [ -z "$x" ]
then
	echo "$1 contains no folders"
else

   for dir in $x;
   do
	recurser "$dir"
   done;

fi;
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

echo 'input top directory (incl. closing / )'
read top
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
recurser $top
recurser()
{
temp="$1"
fix="${temp%/*}"

x=$( ls -1d "$fix"/*/ 2> /dev/null )

[ "$x" = "" ] && echo "$1 contains no folders" && return

echo "$x"  | while read dir
do
   recurser "$dir"
done

}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

echo 'input top directory (incl. closing / )'
read top
echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
recurser "$top"

isdir()
{
  for arg
  do
    [ -d "$arg" ] && return
  done
  return 1
}

recurse()
(
  cd "${1:-.}" || exit 1
  if isdir */
  then
    for dir in */
    do
      recurse "$dir"
    done
  else
    printf "%s\n" "$PWD"
    return
  fi
)

recurse "${1:-.}"

Hi, cfajohnson:

I'm sure it was just an oversight, but the positional parameter needs to be weak quoted. Also, I believe dot directories will be overlooked under most typical posix-like shell environments.

Cheers,
Alister

True.

That's the reason for using dot files and directories -- so that they will be passed over.

Agreed. I simply mentioned it in case that's not the desired behavior for the original poster's situation (although if it is an issue, there's likely a non-standard dotglob option that can be enabled).

Regards,
Alister