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