shell script problem

Polytropon freebsd at edvax.de
Sun Dec 23 09:34:39 UTC 2012


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, ...


More information about the freebsd-questions mailing list