SHELL script of statistical source code line

zhaozj2021-02-16  81

Write a script, you can count the number of rows of the source code by module, huh, huh.

Output results:

~ $ ./line-count

[Module] [LINES] [%]

FRED 4744 17.67%

DataSet 8591 32.00%

Core 9025 33.61%

Image 803 2.99%

TAPE 1339 4.99%

License 16 0.06%

JNI 1621 6.04%

UTIL 264 0.98%

TEST 446 1.66%

Total 26849

The script is as follows:

#! / bin / sh

#

# $ ID: line-count, v 1.2 2004/08/06 08:43:16 CLKRST EXP $

#

# a source-file line counter for C / C / Java

#

# i use the fixed module list instead of searching All Sub-Directory,

# Because it can give me an exact control.

#

# CLKRST@hotmail.com

#

# Count The Lines of Source Files Under The Given Directory

# args: $ 1 The Directory Want To Check.

# $ 2 OPTIONS

Function get_lines () {

Find $ 1 / * $ 2 | xargs wc -l | tail -1 | cut -b1-7

}

# declare the arrays

Declare -a Sources # The Directories to Check

Declare -a names # the module names

Declare -a lines # The Lines of Each Module

# The folowing expresses the line-percentage

# Unit: 0.01

Declare -a person # The inTeger Part

Declare -a Fras # The Fraction Part

# iele you can add as more sources / names as possible

# The source directory

Sources = ("src"

"src / dataset"

"SRC / CORE"

SRC / Image

SRC / TAPE

"src / license"

"jni"

"util")

# THE MODULE NAME OF Each Directory

Names = ("fred"

"dataset"

"core"

"image"

"TAPE"

"license"

"jni"

"util")

# How Many Modules?

# SHOULD BE Equal to the sizeof (Sources) or sizeof (names)

N_MOD = 8 # extra modules

Names [$ n_mod] = "test"

Sources [$ n_mod] = "test"

LINES [$ n_mod] = `Get_Lines $ {sources [$ n_mod]}" -maxdepth 2 -name * .cpp -or -name * .h "`;

N_MOD = $ (($ N_MOD 1)))

# Count The Lines of Each Module

L_total = $ ((0))

For ((i = 0; $ i <$ n_mod; i = $ i 1)); do

LINES [$ I] = `Get_Lines $ {Sources [$ I]}" -maxDepth 2 -Name * .cpp -or -name * .h -or -name * .java "`;

L_total = $ (($ L_TOTAL $ {LINES [$ I]});

DONE

# Calculate the percentage

For ((i = 0; $ i <$ n_mod; i = $ i 1)); do

# The formue is:

# L_lines / l_total * 100 0.5

# Eliminate the float calculation

# (L_lines * 100 l_total / 2) / l_total

# To get fraction part, we use

# FRA (x) = (x * 100) - (x * 100) / 100 * 100

# int (x) = (x * 100) / 100

PERS [$ I] = $ (($ {LINES [$ I]} * 1000 $ L_TOTAL / 2) / $ L_TOTAL));

FRAS [$ I] = $ ($ {PERS [$ I]} - $ {PERS [$ I]} / 100 * 100));

PERS [$ I] = $ (($ {PERS [$ I]} / 100));

DONE

# Output Result

Printf "[Module] / T [line] / t [%%] / N"

For ((i = 0; $ i <$ n_mod; i = $ i 1));

Do Printf "% S / T% 5D / T% 2D.% 02D %% / N" $ {Names [$ I]} $ {person [$ I]} $ {FRAS [$ I]};

DONE

Printf "/ ntotal / t / t% 5d / n" $ l_total

Unset Sources Names Lines Pers

Unset n_mod l_total

转载请注明原文地址:https://www.9cbs.com/read-14502.html

New Post(0)