Computing.Net > Forums > Unix > shell script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

shell script

Reply to Message Icon

Name: mehul
Date: May 23, 2003 at 12:59:10 Pacific
OS: sun
CPU/Ram: sun
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Swapan Satpathi
Date: May 23, 2003 at 14:16:51 Pacific
Reply:

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

Thanks
Swapan


0

Response Number 2
Name: mehul
Date: May 23, 2003 at 16:19:29 Pacific
Reply:

I tried assinging count to pass
but than also it gives syntax error for expr

the expr works for simple addition
z=`expr $x + $y
works fine if x and y are defined

any idea

Mehul


0

Response Number 3
Name: nails
Date: May 23, 2003 at 20:35:54 Pacific
Reply:

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



0

Response Number 4
Name: WilliamRobertson
Date: May 26, 2003 at 11:56:19 Pacific
Reply:

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"
done

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



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: shell script

Shell Scripting www.computing.net/answers/unix/shell-scripting/8475.html

Variables in Shell SCripting www.computing.net/answers/unix/variables-in-shell-scripting/6020.html

shell script to uninstall java www.computing.net/answers/unix/shell-script-to-uninstall-java-/5976.html