From owner-freebsd-questions@FreeBSD.ORG Thu Sep 20 02:29:48 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 3A905106566B for ; Thu, 20 Sep 2012 02:29:48 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx02.qsc.de (mx02.qsc.de [213.148.130.14]) by mx1.freebsd.org (Postfix) with ESMTP id EC1BC8FC08 for ; Thu, 20 Sep 2012 02:29:47 +0000 (UTC) Received: from r56.edvax.de (port-92-195-214-70.dynamic.qsc.de [92.195.214.70]) by mx02.qsc.de (Postfix) with ESMTP id 168B927CBD; Thu, 20 Sep 2012 04:29:46 +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 q8K2TjOT004099; Thu, 20 Sep 2012 04:29:45 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Thu, 20 Sep 2012 04:29:45 +0200 From: Polytropon To: Martin McCormick Message-Id: <20120920042945.ed2edd11.freebsd@edvax.de> In-Reply-To: <201209200203.q8K23Bv5034624@x.it.okstate.edu> References: <201209200203.q8K23Bv5034624@x.it.okstate.edu> 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: FreeBSD Questions 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 02:29:48 -0000 On Wed, 19 Sep 2012 21:03:11 -0500, Martin McCormick wrote: > I just discovered a knowledge deficiency on my part that > I can't seem to resolve. > > If one writes a loop of the following form: > > #!/usr/local/bin/bash Just a sidenote: If you're not using bash-specific functionality and intend to make your script portable, use #!/bin/sh instead. > ls -LF |grep \/ >/tmp/files > while read dirname; do Attention: "dirname" (/usr/bin/dirname) is a binary! > cd $dirname > #Do whatever commands to be repeated in each directory. > done < /tmp/files > > This works quite well but it is shall we say sloppy > because it creates a file that then must be cleaned up and its > name needs to be made unique, etc. 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). > The standard output of the `ls -LF |grep \/` command > needs to look like a file and all should be well. I thought the > < redirection would pickup the standard output. No, the > and < redirections basically operate on files, while pipes redirect strandard output to standard input. So for example, somecommand < /tmp/somefile refers to a file that has to exist, while somecommand < `someothercommand` does not take someothercommand's output (stdout), but instead interprets it as a file specification and then reads from that files (if existing). -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...