[ale] SOLUTION Re: Keep space separated values sane in a bash "for" loop?
Christopher Bergeron
christopher at bergeron.com
Mon Apr 9 18:11:50 EDT 2007
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?
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
>
>
More information about the Ale
mailing list