I need a better way to loop in the shell...
Robert Eckardt
rol at robert-eckardt.de
Tue Dec 13 21:00:31 PST 2005
On Tue, 13 Dec 2005 23:44:42 -0500 (EST), user wrote
> I always do loops in /bin/sh like this:
>
> for f in `cat file` ; do rm -rf $f ; done
Hi,
try instead:
cat file | while read f ; do rm -f "$f" ; done
In your command `file' is presented as
10,000 Maniacs MTV Unplugged - 01 - These Are Days.mp3 10,000 Maniacs MTV
Unplugged - 02 - Eat For Two.mp3 10,000 Maniacs MTV Unplugged - 03 - Candy
Everybody Wants.mp3 ...
while the for-loop cuts the arguments at spaces.
read reads the rest of the line in its last (and only -- in this case)
argument into a single variable.
Regards,
Robert
>
> Easy. I like doing it like this.
>
> The problem is, when I am dealing with an input list that has
> multiple words per line, this chops it up and treats every word as a
> new line...
>
> For instance, lets say I have a file full of filenames, like this:
>
> # cat file
>
> 10,000 Maniacs MTV Unplugged - 01 - These Are Days.mp3
> 10,000 Maniacs MTV Unplugged - 02 - Eat For Two.mp3
> 10,000 Maniacs MTV Unplugged - 03 - Candy Everybody Wants.mp3
>
> and I try to use the above loop on it, it thinks that every word is
> a line ... the above loop will attempt to delete the following files:
>
> 10,000
> Maniacs
> MTV
> Unplugged
> -
> 01
> -
> These
>
> (and so on)
>
> Even if I quote the variable $f, like:
>
> for f in `cat file` ; do rm -rf "$f" ; done
>
> it still does the same thing.
>
> -----
>
> So my question is, what is a nice simple way to loop in the shell, as
> close to what I am doing above as possible, that does not have this
> problem ? Should I just be using something other than `cat` ? Or
> what ?
>
> THanks.
>
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"
--
Dr. Robert Eckardt --- Robert.Eckardt at Robert-Eckardt.de
More information about the freebsd-questions
mailing list