[ale] SOLUTION Re: Keep space separated values sane in a bash "for" loop?
Christopher Bergeron
christopher at bergeron.com
Mon Apr 9 23:24:03 EDT 2007
Jerry - I appreciate your input, but your solution does not work as I
described it. James' solution works; however it doesn't work in an
alias as he described.
Your solution:
find . -maxdepth 1 -mindepth 1 -type d -print0 | sed 's/\ /\\ /g'|sed
's/.\///'| xargs -0 du -sh
does not work as you purported, however, I do appreciate your suggestion.
Kind regards,
CB
Jerry Yu wrote:
> your missed the -print0 for find and -0 for xargs. This combo uses
> NULL to separate fields. Check out my initial reply as well as James's.
>
> On 4/9/07, *Christopher Bergeron* <christopher at bergeron.com
> <mailto:christopher at bergeron.com>> wrote:
>
> Aha!
> James, I think you've _mostly_ solved my problem. I used your
> cmdline
> (which works), however, it didn't work the way I was applying
> it: as an
> alias!
>
> For example:
> find . -maxdepth 1 -mindepth 1 -type d | sed 's/\ /\\ /g'|sed
> 's/.\///'|xargs du -sh == works perfectly
>
> however...
>
> alias diskpie="find . -maxdepth 1 -mindepth 1 -type d | sed 's/\ /\\
> /g'|sed 's/.\///'|xargs du -sh" == does not work
>
> I have it feeling it has something to do with escaping the quotes, but
> I'm surprised that alias mungs the string it runs (actually, I'm not
> really surprised)...
>
> Kind regards,
> CB
>
>
>
>
>
>
> James P. Kinney III wrote:
> > 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 <mailto:Ale at ale.org>
> >>> http://www.ale.org/mailman/listinfo/ale
> >>>
> >>>
> >>>
> >> _______________________________________________
> >> Ale mailing list
> >> Ale at ale.org <mailto:Ale at ale.org>
> >> http://www.ale.org/mailman/listinfo/ale
> >>
> >>
> ------------------------------------------------------------------------
>
> >>
> >> _______________________________________________
> >> Ale mailing list
> >> Ale at ale.org <mailto:Ale at ale.org>
> >> http://www.ale.org/mailman/listinfo/ale
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org <mailto:Ale at ale.org>
> http://www.ale.org/mailman/listinfo/ale
> <http://www.ale.org/mailman/listinfo/ale>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
More information about the Ale
mailing list