In freebsd-questions Digest, Vol 447, Issue 1, Message: 13
On Sun, 23 Dec 2012 18:48:12 +0100 Dh?nin Jean-Jacques <dhe...@gmail.com>
 > 2012/12/23 Polytropon <free...@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
_______________________________________________
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscr...@freebsd.org"

Reply via email to