Re: shell script problem

2012-12-24 Thread Ian Smith
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
___
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"


Re: shell script problem

2012-12-23 Thread  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

>  break
> fi
> done
>

 *  echo " Process: " $$*
# And the parent


>  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

 Best regards

-
(°>   Dhénin Jean-Jacques
/ ) 48, rue de la Justice 78300 Poissy
^^   dhe...@gmail.com
-
___
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"

Re: shell script problem

2012-12-23 Thread  Dhénin Jean-Jacques
2012/12/23 Polytropon 

> On Sun, 23 Dec 2012 10:34:34 +0100, Polytropon wrote:
> > First, the lines with "read" have to be:
> >
> > cat /foo/bar.txt | while read $LINE1
> >
> >   cat /foo/bar/foo/bar.txt | while read $LINE2
> >
> > Reason: $LINE1 and $LINE2 will be evaluated here, they are "empty
> > string", causing "read" to throw an error.
>
> Excuse me - I made a mistake! Of course those two lines
> have to be:
>
> cat /foo/bar.txt | while read LINE1
>
> and
>
> cat /foo/bar/foo/bar.txt | while read LINE2
>
> The $ infront of the variable names have to be removed.
> The variable _name_, not its content, has to be provided
> to "read" as a parameter.
>
> The script so far:
>
>
>
> #!/bin/sh
>
> cat foo.txt | while read LINE1
>

*  echo "Pid Process: " $$*


> do
> cat bar.txt | while read LINE2
> do
> if [ "$LINE1" = "$LINE2" ]; then
> sw="1"
> echo "Current value of sw is : " $sw
>
*ps -ax |grep bar *

>  break
> fi
> done
> echo "Value of sw is : " $sw
> if [ "$sw" = "0" ]; then
> echo "DO SOMETHING!"
> fi
> sw="0"
> done
>



Has you can see,  "pipe" make a subshell and sw is lost.

Hope this help

-
(°>   Dhénin Jean-Jacques
/ ) 48, rue de la Justice 78300 Poissy
^^   dhe...@gmail.com
-
___
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"

Re: shell script problem

2012-12-23 Thread Steve O'Hara-Smith
On Sun, 23 Dec 2012 09:57:02 +
Matthew Seaman  wrote:

> Hmmm I'd just like to draw your attention to the comm(1) program,
> which lets you find lines common to two files, or only in one or other
> of a pair of inputs, very easily.  The only slight gotcha is that the
> input files have to be sorted.

For which purpose the sort program is most useful.

-- 
Steve O'Hara-Smith 
___
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"


Re: shell script problem

2012-12-23 Thread Matthew Seaman
On 23/12/2012 09:43, Polytropon wrote:
> On Sun, 23 Dec 2012 10:34:34 +0100, Polytropon wrote:
>> First, the lines with "read" have to be:
>>
>> cat /foo/bar.txt | while read $LINE1
>>
>>  cat /foo/bar/foo/bar.txt | while read $LINE2
>>
>> Reason: $LINE1 and $LINE2 will be evaluated here, they are "empty
>> string", causing "read" to throw an error.
> 
> Excuse me - I made a mistake! Of course those two lines
> have to be:
> 
> cat /foo/bar.txt | while read LINE1
> 
> and
> 
>   cat /foo/bar/foo/bar.txt | while read LINE2
> 
> The $ infront of the variable names have to be removed.
> The variable _name_, not its content, has to be provided
> to "read" as a parameter.
> 
> The script so far:
> 
> 
> 
> #!/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
>   break
>   fi
>   done
>   echo "Value of sw is : " $sw
>   if [ "$sw" = "0" ]; then
>   echo "DO SOMETHING!"
>   fi
>   sw="0"
> done
> 
> 

Hmmm I'd just like to draw your attention to the comm(1) program,
which lets you find lines common to two files, or only in one or other
of a pair of inputs, very easily.  The only slight gotcha is that the
input files have to be sorted.

Cheers,

Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.
PGP: http://www.infracaninophile.co.uk/pgpkey




signature.asc
Description: OpenPGP digital signature


Re: shell script problem

2012-12-23 Thread Polytropon
On Sun, 23 Dec 2012 10:34:34 +0100, Polytropon wrote:
> First, the lines with "read" have to be:
> 
> cat /foo/bar.txt | while read $LINE1
> 
>   cat /foo/bar/foo/bar.txt | while read $LINE2
> 
> Reason: $LINE1 and $LINE2 will be evaluated here, they are "empty
> string", causing "read" to throw an error.

Excuse me - I made a mistake! Of course those two lines
have to be:

cat /foo/bar.txt | while read LINE1

and

cat /foo/bar/foo/bar.txt | while read LINE2

The $ infront of the variable names have to be removed.
The variable _name_, not its content, has to be provided
to "read" as a parameter.

The script so far:



#!/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
break
fi
done
echo "Value of sw is : " $sw
if [ "$sw" = "0" ]; then
echo "DO SOMETHING!"
fi
sw="0"
done



-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
___
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"


Re: shell script problem

2012-12-23 Thread Polytropon
On Sun, 23 Dec 2012 01:05:35 -0800 (PST), Jack Mc Lauren wrote:
> Hi all
> Please take a look at the script below wich I've wrote :
> 1- cat /foo/bar.txt | while read $LINE12- do3-    cat /foo/bar/foo/bar.txt | 
> while read $LINE24-    do 5-         if [ "$LINE1" = "$LINE2" ]; then6-       
>         sw="1"7-               echo "Current value of sw is : " $sw8-         
>       break9-         fi10-    done11-    echo "Value of sw is : " $sw12-    
> if [ "$sw" = "0" ]; then13-         DO SOMETHING14-    fi15-    sw="0"16- 
> done    

This is totally distorted! Allow me to re-arrange it.



cat /foo/bar.txt | while read $LINE1
do
cat /foo/bar/foo/bar.txt | while read $LINE2
do
if [ "$LINE1" = "$LINE2" ]; then
sw="1"
echo "Current value of sw is : " $sw
break
fi
done
echo "Value of sw is : " $sw
if [ "$sw" = "0" ]; then
DO SOMETHING
fi
sw="0"
done



First, the lines with "read" have to be:

cat /foo/bar.txt | while read $LINE1

cat /foo/bar/foo/bar.txt | while read $LINE2

Reason: $LINE1 and $LINE2 will be evaluated here, they are "empty
string", causing "read" to throw an error.

      


> You probebly guessed what I want to do. But the problem is that
> when the value of sw sets to 1 (in the first if statement) and
> the loop breaks , the value of sw is not '1' anymore in
> " echo "Value of sw is : " $sw " !!!
> Thanks in advance ...

For testing, I've replaced the $sw=0 line with an "echo" command.
I've created two files foo.txt and bar.txt for test, both have
one line in common (3rd line in my example data). If I run the
script, I get this output:

Value of sw is :<- after 1st line (uninitialized)
Value of sw is :  0 <- after 2nd line
DO SOMETHING!
Current value of sw is :  1 <- after 3nd line (common entry)
Value of sw is :  0 <- after 4th line
DO SOMETHING!
Value of sw is :  0 <- after 5th line
DO SOMETHING!

It seems that the condition $LINE1=$LINE2 properly triggers
the "current value" echo command, while all non-common lines
trigger the "DO SOMETHING" action.

If you indended something else, please elaborate.



-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
___
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"