From owner-freebsd-questions@FreeBSD.ORG Wed Dec 14 05:31:33 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 B27CB16A41F for ; Wed, 14 Dec 2005 05:31:33 +0000 (GMT) (envelope-from freebsd@meijome.net) Received: from sigma.octantis.com.au (ns2.octantis.com.au [207.44.189.124]) by mx1.FreeBSD.org (Postfix) with ESMTP id 1277243D4C for ; Wed, 14 Dec 2005 05:31:30 +0000 (GMT) (envelope-from freebsd@meijome.net) Received: (qmail 21537 invoked from network); 14 Dec 2005 16:31:30 +1100 Received: from andromeda.lef.com.au (HELO ?10.168.101.24?) (210.8.93.2) by sigma.octantis.com.au with (DHE-RSA-AES256-SHA encrypted) SMTP; 14 Dec 2005 16:31:30 +1100 Message-ID: <439FAE2D.8000206@meijome.net> Date: Wed, 14 Dec 2005 16:31:25 +1100 From: Norberto Meijome User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923) X-Accept-Language: en-us, en MIME-Version: 1.0 To: user References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: 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:31:33 -0000 user wrote: > I always do loops in /bin/sh like this: > > for f in `cat file` ; do rm -rf $f ; done > > 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 ? you can redefine the character(s) used by your shell for delimiting words after expansion. In Bash , this is the env var IFS. so , if you know there are no ;, you can say export IFS=";" [your loop here] export IFS= and you should be set. man bash for more info. Beto