[ale] SOLUTION Re: Keep space separated values sane in a bash "for" loop?

Geoffrey lists at serioustechnology.com
Mon Apr 9 20:04:14 EDT 2007


I think there is a simpler way of attacking this problem.  It doesn't 
handle file names that end in a space, but other then that, it appears 
to work.  I have a directory that contains the following:

rhwsfoo> ls -l
total 28
drwxr-xr-x  3 esoteric esoteric 4096 Apr  9 20:01 bar
drwxr-xr-x  2 esoteric esoteric 4096 Apr  9 19:19 bar, far
drwxr-xr-x  2 esoteric esoteric 4096 Apr  9 19:19 bar foo
-rwx------  1 esoteric esoteric   72 Apr  9 20:01 fixit
drwxr-xr-x  2 esoteric esoteric 4096 Apr  9 19:33  leading space
drwxr-xr-x  2 esoteric esoteric 4096 Apr  9 19:34 trailing space
drwxrwxr-x  2 esoteric esoteric 4096 Apr  9 19:59   two leading spaces

('trailing space ' above has a trailing space as it's last character)

You can trick the shell into dealing with leading spaces with the following:

find -maxdepth 1 -type d -print |while read fn; do
du -sh "${fn}"
done

Run against the above directory yields:

36K     .
8.0K    ./bar
4.0K    ./bar foo
4.0K    ./bar, far
4.0K    ./ leading space
du: cannot access `./trailing space/': No such file or directory
4.0K    ./  two leading spaces

Grant it, it doesn't handle the trailing space, but it's almost there. :)

-- 
Until later, Geoffrey

Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety.
  - Benjamin Franklin



More information about the Ale mailing list