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

James P. Kinney III jkinney at localnetsolutions.com
Mon Apr 9 19:59:55 EDT 2007


On Mon, 2007-04-09 at 18:11 -0400, Christopher Bergeron wrote:
> This solution doesn't work with "du" for some reason.
> 
> #!/bin/bash
> oldifs=$IFS
> export IFS=","
> for i in `ls -m|sed 's/, /,/g'`
> do
> du -sH \"$i\"
> done
> export IFS=$oldifs
> 
> It appears its turning all of the elements $i into a single variable.
> 
> This code demonstrates:
> 
> #!/bin/bash
> oldifs=$IFS
> export IFS=","
> for i in `ls -m|sed 's/, /,/g'`
> do
> echo du -sh \"$i\"
> done
> export IFS=$oldifs
> 
> 
> What I'm trying to do is create a script that will do "du -sh" on each 
> directory listed in the current directory. This is what I have so far:
> 
> alias diskpie='for i in `\ls -l | grep drwx | tr -s " " | cut -f9 -d " 
> "`; do du -sh $i; done'
> 
> It works great unless it encounters a directory with a space in the name.
> 
> Anyone have any other ideas?
> 

find . -maxdepth 1 -mindepth 1 -type d | sed 's/\ /\\ /g'|sed 's/.\///'|
xargs du -sh

> Kind regards,
> Chris
> 
> 
> 
> 
> 
> 
> 
> 
> Robert Story wrote:
> > On Fri, 6 Apr 2007 12:18:08 -0400 Robert wrote:
> > RS> On Thu, 5 Apr 2007 20:13:06 -0400 aaron wrote:
> > RS> A> Here's technique that might help. I whipped this up for dealing with
> > RS> A> spaced names common to audio file that I wanted to batch convert
> > RS> A> to mp3.  Works with bash on Mac -- should work with Linux bash as
> > RS> A> well. The trick was to pipe the file listing to READ from STDIN instead
> > RS> A> of letting a FOR loop parse the input:
> > RS> 
> > RS> This works great for embedded spaces, but not so much for spaces at the
> > RS> beginning or end of a file name...
> >
> >
> > I've had this same problem for a while, and have always done manual clean-up.
> > After trying these solutions and reading the ls man page, I now have a working
> > solution that handles spaces anywhere in the name.
> >
> > #!/bin/bash
> > oldifs=$IFS
> > export IFS=","
> > for i in `ls -m|sed 's/, /,/g'`
> > do
> >    echo \"$i\"
> > done
> > export IFS=$oldifs
> >
> >
> > Of course, the above will break for files with a ',' in them, but I can live
> > with that... the paranoid could add an initial ls|grep to check and warn for
> > that case...
> > _______________________________________________
> > Ale mailing list
> > Ale at ale.org
> > http://www.ale.org/mailman/listinfo/ale
> >
> >   
> 
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
-- 
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




More information about the Ale mailing list