#Start counting the number of lines of code in the project
total_length = 0
for each $sub_directory in $project_directory
#start counting the number of lines of code in this directory
directory_length = 0
for each $file in $sub_directory
file_length = get_file_length()
#Show file length
echo "File $file : $file_length"
directory_length = $directory_length + $file_length
total_length = total_length + file_length
done
#Show number of lines of code in sub_directory
echo "Directory $sub_directory : $directory_length"
done
#Show number of lines of code in project
echo "Total size of project: $total_length"
Counting lines of source code using Bash?
Hey. I was wondering how do I count the number of lines in a file with a bash script? I'm working on a new project, and I'm new to the Linux/Unix/Ubuntu platforms. I have a little understanding with scripting and using the terminal. My main goal is to have something like this psuedocode:
The specifics you'll need are how to iterate directories and files in a for loop, how to count lines in a file, and basic arithmetic.
I assume some knowledge of BASH syntax, if you need a hand that Google doesn't provide for whatever reason, just ask. You could save yourself a lot of time just using find, by the way.
# This will print directories and files inside of /some/dirfor dir in /some/dir/*do echo $dirdone# This will print each dir (no files) inside of /some/dir# Note the trailing '/'for dir in /some/dir/*/do echo $dirdone# This will output the newline count in /some/file and nothing else# Note that wc is a seperate program, you could do this in just BASH if you mustcat /some/file | wc -l# This will store the output of a command (such as the above) in a variablesome_variable=`echo hello`# Mathsome_variable=$((50/5))
I assume some knowledge of BASH syntax, if you need a hand that Google doesn't provide for whatever reason, just ask. You could save yourself a lot of time just using find, by the way.
Why not use SLOCCount? It's probably available in your package repository.
<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>
Thanks.
I seem to have some trouble with putting the wc results into a variable. When I try
I get a result of 0. The file exists, and I can see the number of lines within a file, but I can't manage to get the result into a variable.
Quote: Original post by Null and Void
# This will output the newline count in /some/file and nothing else
# Note that wc is a seperate program, you could do this in just BASH if you must
cat /some/file | wc -l
# This will store the output of a command (such as the above) in a variable
some_variable=`echo hello`
I seem to have some trouble with putting the wc results into a variable. When I try
fileSize=`wc -l < $thisFile`echo "$thisFile has $((filesize)) lines of code"
I get a result of 0. The file exists, and I can see the number of lines within a file, but I can't manage to get the result into a variable.
Or, apart from SLOCCount, how about cccc? Should be in all major distro repositories.
Quote: Original post by Mitchell314fileSize=`wc -l < $thisFile`echo "$thisFile has $((filesize)) lines of code"
I get a result of 0. The file exists, and I can see the number of lines within a file, but I can't manage to get the result into a variable.
BASH is case-sensitive.
fileSize=`wc -l < $thisFile`echo "$thisFile has $fileSize lines of code"
Quote: Original post by Null and VoidQuote: Original post by Mitchell314fileSize=`wc -l < $thisFile`echo "$thisFile has $((filesize)) lines of code"
I get a result of 0. The file exists, and I can see the number of lines within a file, but I can't manage to get the result into a variable.
BASH is case-sensitive.fileSize=`wc -l < $thisFile`echo "$thisFile has $fileSize lines of code"
Ouch. I'm an idiot. The sad thing is that I knew it was case sensitive.
Thanks. This will help a lot.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement