Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 23 Dec 2012 01:05:35 -0800 (PST)
From:      Jack Mc Lauren <jack.mclauren@yahoo.com>
To:        FreeBSD Global Users Mailing List <freebsd-questions@freebsd.org>
Subject:   shell script problem
Message-ID:  <1356253535.76738.YahooMailClassic@web160101.mail.bf1.yahoo.com>

next in thread | raw e-mail | index | archive | help
Hi all
Please take a look at the script below wich I've wrote :
1- cat /foo/bar.txt | while read $LINE12- do3- =A0 =A0cat /foo/bar/foo/bar.=
txt | while read $LINE24- =A0 =A0do=A05- =A0 =A0 =A0 =A0 if [ "$LINE1" =3D =
"$LINE2" ]; then6- =A0 =A0 =A0 =A0 =A0 =A0 =A0 sw=3D"1"7- =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 echo "Current value of sw is : " $sw8- =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 break9- =A0 =A0 =A0 =A0 fi10- =A0 =A0done11- =A0 =A0echo "Value of sw i=
s : " $sw12- =A0 =A0if [ "$sw" =3D "0" ]; then13- =A0 =A0 =A0 =A0 DO SOMETH=
ING14- =A0 =A0fi15- =A0 =A0sw=3D"0"16- done =A0 =A0 =A0 =A0 =A0
You probebly guessed what I want to do. But the problem is that when the va=
lue of sw sets to 1 (in the first if statement) and the loop breaks , the v=
alue of sw is not '1' anymore in " echo "Value of sw is : " $sw " !!!
Thanks in advance ...
From owner-freebsd-questions@FreeBSD.ORG  Sun Dec 23 09:34:39 2012
Return-Path: <owner-freebsd-questions@FreeBSD.ORG>
Delivered-To: freebsd-questions@freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
 by hub.freebsd.org (Postfix) with ESMTP id 9A83BB94
 for <freebsd-questions@freebsd.org>; Sun, 23 Dec 2012 09:34:39 +0000 (UTC)
 (envelope-from freebsd@edvax.de)
Received: from mx02.qsc.de (mx02.qsc.de [213.148.130.14])
 by mx1.freebsd.org (Postfix) with ESMTP id 51D248FC0A
 for <freebsd-questions@freebsd.org>; Sun, 23 Dec 2012 09:34:39 +0000 (UTC)
Received: from r56.edvax.de (port-92-195-94-87.dynamic.qsc.de [92.195.94.87])
 by mx02.qsc.de (Postfix) with ESMTP id 8B4FF276D5;
 Sun, 23 Dec 2012 10:34:32 +0100 (CET)
Received: from r56.edvax.de (localhost [127.0.0.1])
 by r56.edvax.de (8.14.5/8.14.5) with SMTP id qBN9YYFr002893;
 Sun, 23 Dec 2012 10:34:34 +0100 (CET)
 (envelope-from freebsd@edvax.de)
Date: Sun, 23 Dec 2012 10:34:34 +0100
From: Polytropon <freebsd@edvax.de>
To: Jack Mc Lauren <jack.mclauren@yahoo.com>
Subject: Re: shell script problem
Message-Id: <20121223103434.def7cd9c.freebsd@edvax.de>
In-Reply-To: <1356253535.76738.YahooMailClassic@web160101.mail.bf1.yahoo.com>
References: <1356253535.76738.YahooMailClassic@web160101.mail.bf1.yahoo.com>
Organization: EDVAX
X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2)
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Cc: FreeBSD Global Users Mailing List <freebsd-questions@freebsd.org>
X-BeenThere: freebsd-questions@freebsd.org
X-Mailman-Version: 2.1.14
Precedence: list
Reply-To: Polytropon <freebsd@edvax.de>
List-Id: User questions <freebsd-questions.freebsd.org>
List-Unsubscribe: <http://lists.freebsd.org/mailman/options/freebsd-questions>, 
 <mailto:freebsd-questions-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/freebsd-questions>;
List-Post: <mailto:freebsd-questions@freebsd.org>
List-Help: <mailto:freebsd-questions-request@freebsd.org?subject=help>
List-Subscribe: <http://lists.freebsd.org/mailman/listinfo/freebsd-questions>, 
 <mailto:freebsd-questions-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Sun, 23 Dec 2012 09:34:39 -0000

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- =A0 =A0cat /foo/bar/foo/ba=
r.txt | while read $LINE24- =A0 =A0do=A05- =A0 =A0 =A0 =A0 if [ "$LINE1" =
=3D "$LINE2" ]; then6- =A0 =A0 =A0 =A0 =A0 =A0 =A0 sw=3D"1"7- =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 echo "Current value of sw is : " $sw8- =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 break9- =A0 =A0 =A0 =A0 fi10- =A0 =A0done11- =A0 =A0echo "Value of =
sw is : " $sw12- =A0 =A0if [ "$sw" =3D "0" ]; then13- =A0 =A0 =A0 =A0 DO SO=
METHING14- =A0 =A0fi15- =A0 =A0sw=3D"0"16- done =A0 =A0

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" =3D "$LINE2" ]; then
			sw=3D"1"
			echo "Current value of sw is : " $sw
			break
		fi
	done
	echo "Value of sw is : " $sw
	if [ "$sw" =3D "0" ]; then
		DO SOMETHING
	fi
	sw=3D"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.

 =A0 =A0 =A0


> 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=3D0 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=3D$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.



--=20
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...



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