From owner-freebsd-questions@FreeBSD.ORG Tue Dec 25 04:38:29 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 60CA4CBA for ; Tue, 25 Dec 2012 04:38:29 +0000 (UTC) (envelope-from smithi@nimnet.asn.au) Received: from sola.nimnet.asn.au (paqi.nimnet.asn.au [115.70.110.159]) by mx1.freebsd.org (Postfix) with ESMTP id BB2238FC14 for ; Tue, 25 Dec 2012 04:38:26 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by sola.nimnet.asn.au (8.14.2/8.14.2) with ESMTP id qBP4c3sh071430; Tue, 25 Dec 2012 15:38:03 +1100 (EST) (envelope-from smithi@nimnet.asn.au) Date: Tue, 25 Dec 2012 15:38:03 +1100 (EST) From: Ian Smith To: Dh?nin Jean-Jacques Subject: Re: shell script problem In-Reply-To: Message-ID: <20121225152522.E83325@sola.nimnet.asn.au> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Cc: Polytropon , freebsd-questions@freebsd.org, Jack Mc Lauren X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 25 Dec 2012 04:38:29 -0000 In freebsd-questions Digest, Vol 447, Issue 1, Message: 13 On Sun, 23 Dec 2012 18:48:12 +0100 Dh?nin Jean-Jacques > 2012/12/23 Polytropon > > #!/bin/sh > > > > cat foo.txt | while read LINE1 > > do > > cat bar.txt | while read LINE2 > > do > > if [ "$LINE1" = "$LINE2" ]; then > > sw="1" > > echo "Current value of sw is : " $sw > > > * ps -l | grep $$ * > # see subshell here Yes indeed. > > break > > fi > > done > > > > * echo " Process: " $$* > # And the parent Yep. > > echo "Value of sw is : " $sw > > if [ "$sw" = "0" ]; then > > echo "DO SOMETHING!" > > fi > > sw="0" > > done > > > > I suggest : > > -----------------%><------------------------------------- > > #!/bin/sh > > cat foo.txt | while read LINE1 > do > echo 'One' > $$tmp > cat bar.txt |while read LINE2 > do > if [ "$LINE1" = "$LINE2" ]; then > echo 'ok' > $$tmp > break > fi > done > > if [ `cat $$tmp` = "One" ]; then > echo "One !" > fi > > if [ `cat $$tmp` = "ok" ]; then > echo "ok !" > fi > done Or, to avoid subshell(s) created in pipeline(s), and subsequent loss of variables set in the subshell(s) to their parents, rather than using: cat foo.txt | while read LINE1 [..] cat bar.txt | while read LINE2 [..] done [..] done you can use: while read LINE1 [..] while read LINE2 [..] done < bar.txt [..] done < foo.txt cheers, Ian