Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi
I am getting an syntax error in expr
i am trying to go thru each and every file under one directory and its subdirectory
getting the no of lines in each file and keep on adding it,i need to know the total no of lines of code done
i tried running expr command outside its working fine......any idea am i doing something wrong
#! /bin/sh
count=0
for values in `find .`
do
echo "$values"
linect=`wc -l $values`
echo " line no in $values is $linect"
count=`expr $count + $linect`
echo "total count $count"
done

Hello,
Assign count to avariable..
pass=count
and then
count=`expr $pass + $linect`
I think it will work..Thanks
Swapan

I tried assinging count to pass
but than also it gives syntax error for exprthe expr works for simple addition
z=`expr $x + $y
works fine if x and y are definedany idea
Mehul

Hi:
Your problem probably is the fact that command:
wc -l filename
returns the number of lines and the filename. Either do this:
cat filename|wc -l
or
wc -l filename|awk ' { print $1 }'
Regards,
Nails

If you're not limited to sh, Korn shell does arithmetic much better:
#!/bin/ksh
integer lines count=0
for file in $(find . -type f)
do
lines=$(wc -l $file | awk '{print $1}')
(( count += lines ))
print "$file: $lines lines. Running total: $count"
doneOn my system (FreeBSD) I had to add the awk expression to wc -l in order to strip the filename that wc includes with the output.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |