From owner-freebsd-questions@FreeBSD.ORG Sat Oct 15 22:59:34 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 9598716A41F for ; Sat, 15 Oct 2005 22:59:34 +0000 (GMT) (envelope-from drew@mykitchentable.net) Received: from relay04.roc.ny.frontiernet.net (relay04.roc.ny.frontiernet.net [66.133.182.167]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0EA1643D46 for ; Sat, 15 Oct 2005 22:59:33 +0000 (GMT) (envelope-from drew@mykitchentable.net) Received: from filter01.roc.ny.frontiernet.net (filter01.roc.ny.frontiernet.net [66.133.183.68]) by relay04.roc.ny.frontiernet.net (Postfix) with ESMTP id 2DC75358274; Sat, 15 Oct 2005 22:59:33 +0000 (UTC) Received: from relay04.roc.ny.frontiernet.net ([66.133.182.167]) by filter01.roc.ny.frontiernet.net (filter01.roc.ny.frontiernet.net [66.133.183.68]) (amavisd-new, port 10024) with LMTP id 11409-02-83; Sat, 15 Oct 2005 22:59:33 +0000 (UTC) Received: from blacklamb.mykitchentable.net (67-51-164-214.dsl1.elk.ca.frontiernet.net [67.51.164.214]) by relay04.roc.ny.frontiernet.net (Postfix) with ESMTP id 80A8C3581AB; Sat, 15 Oct 2005 22:59:26 +0000 (UTC) Received: from [192.168.1.3] (unknown [192.168.1.3]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by blacklamb.mykitchentable.net (Postfix) with ESMTP id C6E16154818; Sat, 15 Oct 2005 15:59:25 -0700 (PDT) Message-ID: <435189CD.8080606@mykitchentable.net> Date: Sat, 15 Oct 2005 15:59:25 -0700 From: Drew Tomlinson User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923) X-Accept-Language: en-us, en MIME-Version: 1.0 To: David Kirchner References: <435027A3.8000908@mykitchentable.net> <35c231bf0510141524u133f2d1bkeb46d60e112ee413@mail.gmail.com> In-Reply-To: <35c231bf0510141524u133f2d1bkeb46d60e112ee413@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new-2.3.2 (20050629) at filter01.roc.ny.frontiernet.net Cc: FreeBSD Questions Subject: Re: Help Understanding While Loop 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: Sat, 15 Oct 2005 22:59:34 -0000 On 10/14/2005 3:24 PM David Kirchner wrote: >On 10/14/05, Drew Tomlinson wrote: > > >>OK, I've been working on an sh script and I'm almost there. In the >>script, I created a 'while read' loop that is doing what I want. Now I >>want to keep track of how many times the loop executes. Thus I included >>this line between the 'while read' and 'done' statements: >> >>count = $(( count + 1 )) >> >>I've tested this by adding an 'echo $count' statement in the loop and it >>increments by one each time the loop runs. However when I attempt to >>call $count in an 'echo' statement after the 'done', the variable is >>null. Thus I assume that $count is only local to the loop and I have to >>export it to make it available outside the loop? What must I do? >> >> > >Oh yeah, that's another side effect of using the while read method. >Because it's "| while read" it's starting a subshell, so any variables >are only going to exist there. You'd need to have some sort of 'echo' >within the while read, and then | wc -l at the end of the while loop, >or something along those lines. > >The IFS method someone else mentioned, in regards to 'for' loops, >would probably be better all around. So you'd want: > >OLDIFS=$IFS ># Note this is a single quote, return, single quote, no spaces >IFS=' >' > >for i in `find etc` >do >done > >IFS=$OLDIFS > > OK, I've tried this and it does fix the "count" problem. However it messes up another part of the script and I'm trying understand why. I tried to make this script dynamic in that all I would need to do is edit variables set at the top and then not have to worry about all occurrences in the script. Thus I set the following variables: remote_pictures_dir="/multimedia/Pictures" local_pictures_dir="/tv/pictures" find_args="-iname '*.jpg' -or -iname '*.gif'" Then I called the 'find' command as follows: for original in $(/usr/bin/find $remote_pictures_dir $find_args -print) But when I run my script, I get "/usr/bin/find: invalid predicate `-iname '*.jpg' -or -iname '*.gif''". However if I don't try and use $find_args and type the arguments in specifically, the script runs fine. I tried various combinations of quoting and escaping those quotes but can't come up with a combination that works. What is going on? And is there some way to set verbosity so I can see how the shell is expanding the variables? Thanks much, Drew -- Visit The Alchemist's Warehouse Magic Tricks, DVDs, Videos, Books, & More! http://www.alchemistswarehouse.com