simulation of wc command giving problems

Hello,
i am developing a simple shell script for simulation of wc command but it's not working.Please tell me what is the problem. I have commented the problematic line

if [ $# -lt 1 ]
then
	echo "<Improper Usage>"
	echo "wcassg <options> file"
else
	eval file=\$$#
	if [ -f $file ]
	then
		chars=0
		words=0
		lines=0
		if [ $# -eq 1 ]
		then
			exec<$file
			while read sentence
			do
#the line below this is not working i dont know i am not able to use string functions on $sentence
				chars=`expr $chars + \`expr length $sentence\``
				set $sentence
				lines=`expr $lines + 1`
				words=`expr $words + $#`
			done
			echo "Number Of Lines :$lines"
			echo "Number Of Words :$words"
			echo "Number Of Characters :$chars"
		fi
	else
		echo "$file does not exists"
	fi
fi


That fails if there are more than 9 arguments. Use:

eval "file=\${$#}"

Why can't you use string functions on $sentence?

There's no need to use an external command to do integer arithmetic:

chars=$(( $chars + ${#sentence} ))

That may fail in various ways, depending on the contents of $sentence. Use:

set -f
set -- $sentence
set +f
lines=$(( $lines + 1 ))
words=$(( $words + $# ))

gosh! cfajohnson you did the operation of my whole program. :slight_smile:
Thanks a lot! for pointing out the mistakes