From owner-freebsd-questions@FreeBSD.ORG Wed Dec 14 05:00:31 2005 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 76D3416A41F; Wed, 14 Dec 2005 05:00:31 +0000 (GMT) (envelope-from rol@robert-eckardt.de) Received: from natfrord.rzone.de (natfrord.rzone.de [81.169.145.161]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9F6F543D5E; Wed, 14 Dec 2005 05:00:24 +0000 (GMT) (envelope-from rol@robert-eckardt.de) Received: from postit (p54B12B30.dip0.t-ipconnect.de [84.177.43.48]) (authenticated bits=0) by post.webmailer.de (8.13.1/8.13.1) with ESMTP id jBE50EMx000032; Wed, 14 Dec 2005 06:00:15 +0100 (MET) Received: from www.eckardt.org (localhost.eckardt.org [127.0.0.1]) by postit (8.12.10/8.12.10) with ESMTP id jBE50FDh037898; Wed, 14 Dec 2005 06:00:15 +0100 (CET) (envelope-from rol@robert-eckardt.de) From: "Robert Eckardt" To: user , freebsd-questions@freebsd.org Date: Wed, 14 Dec 2005 06:00:15 +0100 Message-Id: <20051214045249.M61301@Robert-Eckardt.de> In-Reply-To: References: X-Mailer: Open WebMail 2.21 20031110 X-OriginatingIP: 192.168.182.11 (roberte) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Cc: owner-freebsd-questions@freebsd.org Subject: Re: I need a better way to loop in the shell... X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Dec 2005 05:00:31 -0000 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@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" -- Dr. Robert Eckardt --- Robert.Eckardt@Robert-Eckardt.de