[SunHELP] help with script

sunhelp at sunhelp.org sunhelp at sunhelp.org
Fri Nov 2 06:47:08 CST 2001


The Bourne shell executes loops in a subshell if the input or output is
redirected.  The variable you are setting exists in the subshell.  If you
change this to run in Korn shell it may work for you.

However, there's a much easier way of counting lines using wc -l.

--------------------------
#!/bin/sh

for FILE in test.*.txt
do
	wc -l $FILE
done
---------------------------

If you don't want the filenames in the listing, change the wc line to:
wc -l | awk '{ print $1 }'

-----
Nathan Nichols
Unix System Administrator
Cicada - http://www.cicadacorp.com/




-----Original Message-----
From: Gavin Winter [mailto:gwinter at rlo.com.au]
Sent: Friday, November 02, 2001 2:08 AM
To: sunhelp at sunhelp.org
Subject: [SunHELP] help with script


Hello all,
	I have a problem with the following script under Solaris 8 on Sparc:
I am
trying to count the lines in a file but the output of 'linecount' is always
'0'. When I run the script with the -x option I can see linecount being
incremented. Is this a scope problem. Can someone give me some advice here?

regards

Gavin Winter



#!/bin/sh

linecount=0

for FILE in `ls test.*.txt`
do
	while read xx
 	do
		linecount=`expr $linecount + 1`
	done < $FILE
  	echo $linecount
done

_______________________________________________
SunHELP maillist  -  SunHELP at sunhelp.org
http://www.sunhelp.org/mailman/listinfo/sunhelp



More information about the SunHELP mailing list