From owner-freebsd-questions@FreeBSD.ORG Thu Sep 20 15:26:21 2012 Return-Path: Delivered-To: freebsd-questions@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id A8896106566B for ; Thu, 20 Sep 2012 15:26:21 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de (mx01.qsc.de [213.148.129.14]) by mx1.freebsd.org (Postfix) with ESMTP id 65ED18FC1B for ; Thu, 20 Sep 2012 15:26:21 +0000 (UTC) Received: from r56.edvax.de (port-92-195-214-70.dynamic.qsc.de [92.195.214.70]) by mx01.qsc.de (Postfix) with ESMTP id 75D1D3CAFA; Thu, 20 Sep 2012 17:26:19 +0200 (CEST) Received: from r56.edvax.de (localhost [127.0.0.1]) by r56.edvax.de (8.14.5/8.14.5) with SMTP id q8KFQIVD001990; Thu, 20 Sep 2012 17:26:18 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Thu, 20 Sep 2012 17:26:18 +0200 From: Polytropon To: Jan Henrik Sylvester Message-Id: <20120920172618.607c8ed3.freebsd@edvax.de> In-Reply-To: <505ADEF8.9080504@janh.de> References: <201209200203.q8K23Bv5034624@x.it.okstate.edu> <20120920042945.ed2edd11.freebsd@edvax.de> <505ADEF8.9080504@janh.de> Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: Martin McCormick , questions-list freebsd Subject: Re: bash Shell Scripting Question X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Polytropon List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 Sep 2012 15:26:21 -0000 On Thu, 20 Sep 2012 11:16:40 +0200, Jan Henrik Sylvester wrote: > On 09/20/2012 04:29, Polytropon wrote: > > Correct. You could use different approaches which may or may > > not fail due to the directory names you will encounter (like > > directories with spaces or special characters). > > > > #!/bin/sh > > for DIR in `ls -LF | grep \/`; do > > cd ${DIR} > > # do stuff > > done > > > > Or you can use piping: > > > > #!/bin/sh > > ls -LF | grep \/ | while read DIR; do > > cd ${DIR} > > # do stuff > > done > > > > I'm quite confident there are even more elegant and fault- > > tolerant solutions. You would maybe have to tweak the ls > > command or play with IFS (space or newline). > > Even if you start quoting "${DIR}", the first one will fail at least for > names containing spaces, the second one at least for names starting with > spaces. As you said, you would have to change IFS to maybe slash and > newline, assuming that you do not have names containing newlines, in > which case the approach cannot work. You are fully correct: In order to create an iterator that provides "valid" directory names, even for the cases where "unusual" characters (which are _valid_ characters for file names and directory names) are included, is not trivial. Allow me to point to those two articles which mention different approaches and show why they are wrong. :-) David A. Wheeler: Filenames and Pathnames in Shell: How to do it correctly http://www.dwheeler.com/essays/filenames-in-shell.html David A. Wheeler: Fixing Unix/Linux/POSIX Filenames: Control Characters (such as Newline), Leading Dashes, and Other Problems http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html > I understand that you want all directories and links to directories not > starting with a period. How about trying all files not starting with a > period and skipping the non directories: > > #!/bin/sh > for DIR in * > do > cd "$DIR" >/dev/null 2>&1 || continue > pwd > cd - > /dev/null > done > > This one works with names containing spaces or even newlines and does > not even need to spawn external commands or subshells. It may have other > caveats, though. It will work - it delegates resolving * to the shell instead of having a different program (ls | grep, find) doing that. -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...