Script using find to count lines of code
I'm trying to create a shell script that will count the number of lines of code in one folder.
I got this:
h=find . -type f -name \*.[h]* -print0 | xargs -0 cat | wc -l
m=find . -type f -name \*.[m]* -print0 | xargs -0 cat | wc -l
expr $m + $h
But when I'm trying to run it I get this:
lines-of-code: line 6: .: -t: invalid option
.: usage: . filename [arguments]
0
lines-of-code: line 7: .开发者_Python百科: -t: invalid option
.: usage: . filename [arguments]
0
+
I know I have to do something to make it run on the specific folder I'm in. Is this even possible?
DDIYS (don't to it your self) Use cloc instead. Excelent tool written in perl that does the counting for you as well as a other things. It recognizes more than 80 languages.
Example output:
prompt> cloc perl-5.10.0.tar.gz
4076 text files.
3883 unique files.
1521 files ignored.
http://cloc.sourceforge.net v 1.50 T=12.0 s (209.2 files/s, 70472.1 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Perl 2052 110356 130018 292281
C 135 18718 22862 140483
C/C++ Header 147 7650 12093 44042
Bourne Shell 116 3402 5789 36882
Lisp 1 684 2242 7515
make 7 498 473 2044
C++ 10 312 277 2000
XML 26 231 0 1972
yacc 2 128 97 1549
YAML 2 2 0 489
DOS Batch 11 85 50 322
HTML 1 19 2 98
-------------------------------------------------------------------------------
SUM: 2510 142085 173903 529677
-------------------------------------------------------------------------------
Quote the commands like:
h=$(find . -type f -name *.[h]* -print0 | xargs -0 cat | wc -l)
Please also have a look at sloccount for counting lines of code. You can install it on debian/ubuntu with sudo apt-get install sloccount
For this specific problem, I have a different solution:
find . -type f -print0 | wc --files0-from=-
May be I misunderstood the question, but does this work for you?
wc -l *.[mh]*
Now it works!
h=$(find . -type f -name \*.[h]* -print0 | xargs -0 cat | wc -l)
m=$(find . -type f -name \*.[m]* -print0 | xargs -0 cat | wc -l)
expr $m + $h
精彩评论