[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



On Mon, 2007-04-09 at 20:04 -0400, Geoffrey wrote:
> 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. :)
> 
I ran the 
find . -maxdepth 1 -mindepth 1 -type d -print0 | xargs -0 du -sh

against a temp dir with the following:
 ls -1F
betty, bam bam/
dino, pebbles and that cat /
fred/
fred barney/
fred barney wilma /

So I have spaces, commas, ending spaces.

[jkinney at merlin test1]$ find . -maxdepth 1 -mindepth 1 -type d -print0 |
xargs -0 du -sh
4.0K    ./fred barney
4.0K    ./dino, pebbles and that cat
4.0K    ./fred barney wilma
4.0K    ./fred
4.0K    ./betty, bam bam



-- 
James P. Kinney III          
CEO & Director of Engineering 
Local Net Solutions,LLC        
770-493-8244                    
http://www.localnetsolutions.com

GPG ID: 829C6CA7 James P. Kinney III (M.S. Physics)
<jkinney at localnetsolutions.com>
Fingerprint = 3C9E 6366 54FC A3FE BA4D 0659 6190 ADC3 829C 6CA7
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part