1. 程式人生 > >計算程式碼行數的shell指令碼

計算程式碼行數的shell指令碼

用來統計程式碼行數的一個shell指令碼

不過只支援 ./  ./dir  ./dir/dir 三級目錄

有需要的簡單可以用一下

#! /bin/bash
#程式碼書行數統計指令碼,只支援兩級目錄
all_count=0; 	#全部行數統計
file_count=0; 	#檔案行數統計
filename="";
string="";
path=`pwd`;
for filename in `ls`;do
	file_info=`ls -l | grep ${filename}`;
	class=`echo ${file_info:0:1}`;
	#目錄
	if [[ ${class} = 'd' ]];then
		cd ${filename};
		path=`pwd`;
		for filename in `ls`; do
			file_info=`ls -l | grep ${filename}`;
			class=`echo ${file_info:0:1}`;
			
			#第1級目錄
			if [[ ${class} = 'd' ]]; then
				cd ${filename};
				path=`pwd`;
					
					for filename in `ls`; do
						file_info=`ls -l | grep ${filename}`;
						class=`echo ${file_info:0:1}`;
						#第2級目錄
						if [[ ${class} = "d" ]]; then
						#do nothing
							echo "";
						#第2級檔案
						else
							string=`wc -l ${filename}`;
							file_count=`echo ${string} |cut -d' ' -f 1`;
							echo "line_count:${file_count}		filename:${path}/${filename}";
							let  "all_count=file_count+all_count";
						fi
					done
					
				cd ..;
				path=`pwd`;
			#第1級檔案
			else
				string=`wc -l ${filename}`;
				file_count=`echo ${string} |cut -d' ' -f 1`;
				echo "line_count:${file_count}		filename:${path}/${filename}";
				let  "all_count=file_count+all_count";
			fi	
		done
		
		cd ..;
		path=`pwd`;
	#檔案
	else
		string=`wc -l ${filename}`;
		file_count=`echo ${string} |cut -d' ' -f 1`;
		echo "line_count:${file_count}		filename:${path}/${filename}";
		let  "all_count=file_count+all_count";
	fi
done
echo "total_line_count:${all_count}";