shell script problem
Ian Smith
smithi at nimnet.asn.au
Tue Dec 25 04:38:29 UTC 2012
In freebsd-questions Digest, Vol 447, Issue 1, Message: 13
On Sun, 23 Dec 2012 18:48:12 +0100 Dh?nin Jean-Jacques <dhenin at gmail.com>
> 2012/12/23 Polytropon <freebsd at 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
More information about the freebsd-questions
mailing list