for loop troubles


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop troubles
# 1  
Old 03-24-2010
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?

Code:
#!/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

# 2  
Old 03-25-2010
Code:
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"


Last edited by kshji; 03-25-2010 at 12:22 AM..
# 3  
Old 03-25-2010
Code:
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:-.}"


Last edited by cfajohnson; 03-25-2010 at 04:18 AM.. Reason: Fixed quoting of variables
# 4  
Old 03-25-2010
Hi, cfajohnson:

Quote:
Originally Posted by cfajohnson
Code:
recurse()
(
  cd ${1:-.} || exit 1

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

Last edited by alister; 03-25-2010 at 03:05 AM..
# 5  
Old 03-25-2010
Quote:
Originally Posted by alister
I'm sure it was just an oversight, but the positional parameter needs to be weak quoted.
True.
Quote:
Also, I believe dot directories will be overlooked under most typical posix-like shell environments.
That's the reason for using dot files and directories -- so that they will be passed over.
# 6  
Old 03-25-2010
Quote:
Originally Posted by cfajohnson
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Troubles with OpenSSH

Hi, I am trying to login from one AIX server to another without using a password, a basic configuration, however it doesn't seem to work. All things are in place. I have both a public and private key in the ~/.ssh folder and also have an "authorized_keys" file on the target-server containing... (5 Replies)
Discussion started by: Hille
5 Replies

2. Shell Programming and Scripting

awk and tr troubles

I am trying to make all the fields containing lower case letters upper case and the third field of a file display ** instead. I have this: awk '{printf "%s %s ** %d %d\n", $1, $2, $4, $5}' database.txt | tr '' '' < database.txt And that only changes it to upper case, other... (4 Replies)
Discussion started by: Bungkai
4 Replies

3. Shell Programming and Scripting

Encoding troubles

Hello All I have a set of files, each one containing some lines that follows that regex: regex='disabled\,.*\,\".*\"'and here is what file says about each files: file <random file> <random file> ASCII text, with CRLF line terminatorsSo, as an example, here is what a file ("Daffy Duck - The... (3 Replies)
Discussion started by: tukuyomi
3 Replies

4. Shell Programming and Scripting

if-statement troubles

I try to compare the day and month of someones birthday with the day and month of today, so my little bash script can send a mail to the person that has its birthday that day. The first line of the file birthdays looks like this: firstname,lastname,01/01/1990,.... The variable birthday's... (4 Replies)
Discussion started by: doc.arne
4 Replies

5. HP-UX

cron troubles

I have a cronjob that I need to run everyday and it needs to have todays date inputed, here is what I have, but is not working as expected.......... 23 02 * * * cd /path;./RequestSummaryReport.sh $(date +%Y-%m-%d) the output from mail gives me............. Date: Fri, 8 Feb 2008 02:12:07... (4 Replies)
Discussion started by: theninja
4 Replies

6. UNIX for Dummies Questions & Answers

ssh2 troubles

I'm trying to set up a secure and trusted connection between 2 boxes running solaris using ssh2. I've run ssh-keygen2 on the local box and on the remote box, created the identification file ( IdKey id_dsa_2048_a ) on the local machine and copied across the public key file from the local to... (5 Replies)
Discussion started by: PaulC
5 Replies

7. UNIX for Dummies Questions & Answers

Password Troubles

I'm very new to UNIX (I just started working with Terminal 2 days ago) and I don't know the system very well. I'm having trouble whenever I am asked for a password. I simply... can't type. I press keys on the keyboard but no characters appear on the screen. For example, when I log onto... (5 Replies)
Discussion started by: alexmiller
5 Replies

8. Programming

Troubles with HPUX

Hello I created an application in c language for HP-UX operative system,and it runs on a 32 bits PARISC processor. My problem is that I have to run this same application but now in a 64 bits Parisc processor. But I am not able to compile the application with the 64 bit server, and I only could use... (1 Reply)
Discussion started by: masterboy6666
1 Replies

9. UNIX for Dummies Questions & Answers

compariosn troubles...

Hi Guys, I am trying to compare using if, but keep getting some strange results. if ; then keeps creating the file 1 if ; then does not work at all if ; then does not work if ; then does not work if ; then does not work eihter. I am using a ksh, on Solaris (9 Replies)
Discussion started by: jagannatha
9 Replies

10. UNIX Desktop Questions & Answers

NetBSD Troubles

HI, I just installed NetBSD i386 v. latest. I am having a few problems: For some reason X just won't start!? Another is I can't access My CDROM or Floppy. Thanks in advance! (3 Replies)
Discussion started by: vertigo
3 Replies
Login or Register to Ask a Question