From owner-freebsd-questions@FreeBSD.ORG Thu Sep 20 09:16:53 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 4DB29106564A for ; Thu, 20 Sep 2012 09:16:53 +0000 (UTC) (envelope-from me@janh.de) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.17.10]) by mx1.freebsd.org (Postfix) with ESMTP id CF77E8FC14 for ; Thu, 20 Sep 2012 09:16:52 +0000 (UTC) Received: from nb981.math (31-18-156-120-dynip.superkabel.de [31.18.156.120]) by mrelayeu.kundenserver.de (node=mrbap1) with ESMTP (Nemesis) id 0LqFBg-1TjGni2g88-00dnV7; Thu, 20 Sep 2012 11:16:50 +0200 Message-ID: <505ADEF8.9080504@janh.de> Date: Thu, 20 Sep 2012 11:16:40 +0200 From: Jan Henrik Sylvester User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:15.0) Gecko/20120910 Thunderbird/15.0.1 MIME-Version: 1.0 To: Polytropon , Martin McCormick References: <201209200203.q8K23Bv5034624@x.it.okstate.edu> <20120920042945.ed2edd11.freebsd@edvax.de> In-Reply-To: <20120920042945.ed2edd11.freebsd@edvax.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:Pez0WDih/Hp6r7bmEleD9GQ8x0hP70pCHpT0mtKr6aq gxbH1+SAbweTv5dm44/OwM2ePCn6draIg7DACM5UXb815dZuMg EcEVj+2DJI5E1f2doettNN2lFcz+87zAUvUYXlBDsZ8kRl4KFw MEOxKxChOo7UnuiqOJ8NG8QNH7G4onXz1QcPscoOhKy8kzWTLv u8dvTGonc8B+Gba9MsEDYkGTqHKeNablwn7kqOdHnc3xXclufD hMjsnbqFO2fpeDycQSs0T4uJGH0uP6qUkYfq0u2Yk/7vFa+IR2 D2qtvde2kD12xcxqpwdYNf21v/YdVgZZLlCX7f7eqHfFGyjVg= = Cc: questions-list freebsd Subject: Re: Re: bash Shell Scripting Question 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: Thu, 20 Sep 2012 09:16:53 -0000 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. 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. Cheers, Jan Henrik