Welcome, guest | Sign In | My Account | Store | Cart

This bash script enables you to calculate the total number of lines of source code.

Example usage: 1. calculate total number of lines of java source files. $ line.sh java

  1. calculate total number of lines of python source files. $ line.sh py
Bash, 16 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/bash
# @date: May 23, 2011
# @author: Shao-Chuan Wang
# Example usage:
# calculate total number of lines of java source files.
# $ line.sh java
# calculate total number of lines of python source files.
# $ line.sh py

line=0
for f in `find . -type f -name "*.$1"`
do
	li=`wc -l $f | awk '{print $1}'`
	let "line = $line + $li"
done
echo "Total lines: $line"

2 comments

Max Zhuang 12 years, 3 months ago  # | flag

I'd like to write it as following:

if [ $# -ne 1 ]; then
    echo "$0: calculate total number of lines of source files"
    echo "Usage: $0 extension"
    exit
fi

line=0
for f in `find . -type f -name "*.$1"`; do
    li=`wc -l < "$f"`
    let line=line+li
done

echo "Total lines: $line"
Shao-chuan Wang (author) 12 years, 3 months ago  # | flag

Hey, Max. Thank for the feedbacks. =)

Created by Shao-chuan Wang on Mon, 23 May 2011 (MIT)
Bash recipes (41)
Shao-chuan Wang's recipes (22)

Required Modules

  • (none specified)

Other Information and Tasks