2019-08-13 11:23:27 4176瀏覽
本篇文章扣丁學(xué)堂Linux培訓(xùn)小編給讀者們分享一下shell 命令統(tǒng)計(jì)代碼行數(shù)的簡單代碼,文中給大家提到了統(tǒng)計(jì)行數(shù)、字符數(shù)、字節(jié)數(shù)的shell腳本,對Linux開發(fā)技術(shù)感興趣的小伙伴可以參考下。
分享一個(gè)統(tǒng)計(jì)代碼行的shell命令:
根據(jù)不同的項(xiàng)目類型,不同的目錄,自行修改。
如果內(nèi)容多,可以:
補(bǔ)充:下面看下統(tǒng)計(jì)行數(shù)、字符數(shù)、字節(jié)數(shù)的shell腳本
在linux中,有一個(gè)常用的命令wc,可用來統(tǒng)計(jì)文件的行數(shù)、單詞數(shù)、字符數(shù)以及字節(jié)數(shù)。
運(yùn)行wc命令時(shí),必須明確指定命令中的子文件夾的層級。
通常情況下,用于統(tǒng)計(jì)的文件夾,會有很多的層級,會使統(tǒng)計(jì)變得復(fù)雜。
本文提供的這個(gè)腳本,可以幫助我們搜索各級子文件夾,并給出每個(gè)文件夾級別的計(jì)數(shù),并將結(jié)果存儲在一個(gè)單獨(dú)的文件,以供將來使用。
代碼:
代碼示例:
find . “(” -name “.java” -or -name “.html” -or -name “.js” -or -name “.css” “)” -print | xargs wc -l
find . “(” -name “.java” -or -name “.html” -or -name “.js” -or -name “.css” “)” -print | xargs wc -l > statistics.txt
#!/bin/bash
#filename: count_lines.sh
len=$(echo $1 |awk '{print length($0)}')
len2=$(echo $2 |awk '{print length($0)}')
if [ $len -ne "0" ]
then
x="$1"
i="1"
cc=0
while [ $i -le "$len" ]
do
echo `expr substr $1 $i 1 `
if [ `expr substr $1 $i 1 ` = "l" ] || [ `expr substr $1 $i 1 ` = "w" ] || [ `expr substr $1 $i 1 ` = "c" ] || [ `expr substr $1 $i 1 ` = "k" ]
then
lengh = "length=$(echo "$1" |awk '{print length($0)}')"
else
cc=`expr $cc + 1`
fi
i=`expr $i + 1`
done
if [ $cc -eq "0" ]
then
rm ~/count_output
echo "=======================current Directory==========================" >> count_output
pwd >> count_output
if [ $len2 -eq "0" ]
then
ls -l * | wc -$1 *.* | tail -k >> ~/count_output
else
ls -l * | wc -$1 *.$2 | tail -k >> ~/count_output
fi
ls -R | grep './' > list
sed "s/:/ /" list >> list1
cat list1 | while read current_dir
do
echo "=======================$current_dir ==========================" >> ~/count_output
cd $current_dir
if [ $len2 -eq "0" ]
then
ls -l * | wc -$1 *.* | tail -k >> ~/count_output
else
ls -l * | wc -$1 *.$2 | tail -k >> ~/count_output
fi
cd -
done
clear
rm list
rm list1
else
print "invalid entry"
fi
else
print "input options l w c k needs to be entered"
fi
想要了解更多關(guān)于Linux開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學(xué)堂Linux培訓(xùn)官網(wǎng)、微信等平臺,扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的Linux講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的Linux視頻教程定能讓你快速掌握Linux從入門到精通開發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂Linux技術(shù)交流群:422345477。
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入Python全棧開發(fā)免費(fèi)公開課】