Shell programming--Number of statistics code lines

Source: Internet
Author: User

Cited: Before all too lazy, not in time to learn the knowledge summarized into the blog. Recently learning shell programming is just the beginning of a habit of blogging. Usually we will encounter a problem, after writing a project, want to count the total amount of code, when the code file a lot, a point to open code files, and then add up the number of lines of code is a very difficult thing. The shell can solve this problem in just a few lines of code.

A shell source (code_linage.sh):

1 #!/bin/bash23if [$#-eq 1] && [-D$4  then5     find $1-type f-name "*.java"-print0 | xargs-0 WC-l6Else
   
    7     echo ' wrong parameter '
    8 fi
   

Two Shell Code Analysis:

The function of this code is: count the number of rows of Java code in a folder

Line 1th: Although the beginning is #, but this is not a comment, it means to use/bin/bash to explain the script.

Line 2nd: The function is to determine whether there is only one parameter, and the parameter represents a directory. $# represents the number of arguments, to be equal to 1;-d $ is to determine whether the first parameter represents a directory.

Line 5th: Is the core part of this code.        This line can be divided into two parts, and the command "|" in front of the pipeline commands the Find command function to look up all Java files in the directory (also known as the first parameter). The-print0 indicates that these found file names are separated by \ n; the command function after "|" is the number of lines of code that will be counted for each file. Xargs can convert standard input data to command-line parameters and pass it to the WC command. 0 means that the wc-l is the number of code statistics lines.

Three-running effect:

Html_parer5 is a folder of Java projects, the number preceding each line represents the number of lines of code for this Java file, followed by the file name, and the last line is the total amount of code.

Of course, if you want to count the C code, or a variety of other code lines, you can modify the next find command in the-name after the matching string.

Shell programming--Number of statistics code lines

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.