Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Dec 2012 15:38:03 +1100 (EST)
From:      Ian Smith <smithi@nimnet.asn.au>
To:        Dh?nin Jean-Jacques <dhenin@gmail.com>
Cc:        Polytropon <freebsd@edvax.de>, freebsd-questions@freebsd.org, Jack Mc Lauren <jack.mclauren@yahoo.com>
Subject:   Re: shell script problem
Message-ID:  <20121225152522.E83325@sola.nimnet.asn.au>
In-Reply-To: <mailman.95.1356350402.35494.freebsd-questions@freebsd.org>
References:  <mailman.95.1356350402.35494.freebsd-questions@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
In freebsd-questions Digest, Vol 447, Issue 1, Message: 13
On Sun, 23 Dec 2012 18:48:12 +0100 Dh?nin Jean-Jacques <dhenin@gmail.com>
 > 2012/12/23 Polytropon <freebsd@edvax.de>

 > > #!/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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20121225152522.E83325>