這篇文章主要講解了“如何用Shell腳本統(tǒng)計(jì)當(dāng)前目錄下目錄和文件的數(shù)量”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“如何用Shell腳本統(tǒng)計(jì)當(dāng)前目錄下目錄和文件的數(shù)量”吧!
下面用shell寫一個(gè)腳本,放置在當(dāng)前目錄下,執(zhí)行即可。
代碼如下:
#!/bin/bash
#腳本名稱 dir
#定義一個(gè)函數(shù)fun_directory
fun_directory() {
let "filenum=0"
let "dirnum=0"
for i in $( ls )
do
if [ -d $i ]
then
let dirnum+=1
else
let filenum+=1
fi
done
echo "The number of directorys is $dirnum"
echo "The number of files is $filenum"
}
#調(diào)用函數(shù)
fun_directory
我們測(cè)試一下:
代碼如下:
[root@localhost scripts]# pwd
/root/scripts
[root@localhost scripts]# ll |sort
drwxr-xr-x 2 root root 4096 06-12 10:44 charpter8
drwxr-xr-x 2 root root 4096 06-13 12:34 aaa
-rw-r--r-- 1 root root 105 06-13 08:56 file1
-rw-r--r-- 1 root root 106 06-12 14:24 8-9
-rw-r--r-- 1 root root 121 06-12 09:36 jiu
-rw-r--r-- 1 root root 133 06-13 11:09 temp
-rw-r--r-- 1 root root 210 06-12 13:40 8-8
-rw-r--r-- 1 root root 222 06-12 11:51 8-6
-rw-r--r-- 1 root root 247 06-12 11:35 8-5
-rw-r--r-- 1 root root 273 06-12 13:13 8-7
-rw-r--r-- 1 root root 292 06-12 10:57 8-1
-rw-r--r-- 1 root root 309 06-12 14:51 8-11
-rw-r--r-- 1 root root 314 06-12 15:01 8-17
-rw-r--r-- 1 root root 317 06-13 12:10 test
-rw-r--r-- 1 root root 51 06-12 11:00 8-2
-rw-r--r-- 1 root root 53 06-13 08:51 file
-rw-r--r-- 1 root root 67 06-13 10:17 10-4
-rw-r--r-- 1 root root 78 06-13 10:09 test.out
-rwxr-xr-x 1 root root 103 06-12 11:06 8-3
-rwxr-xr-x 1 root root 124 06-13 10:02 10-32
-rwxr-xr-x 1 root root 304 06-13 12:47 dir
-rwxr-xr-x 1 root root 316 06-12 11:21 8-4
#執(zhí)行腳本
[root@localhost scripts]# sh dir
The number of directorys is 2
The number of files is 20
[root@localhost scripts]#
可以看到,數(shù)據(jù)統(tǒng)計(jì)是準(zhǔn)確的。
感謝各位的閱讀,以上就是“如何用Shell腳本統(tǒng)計(jì)當(dāng)前目錄下目錄和文件的數(shù)量”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)如何用Shell腳本統(tǒng)計(jì)當(dāng)前目錄下目錄和文件的數(shù)量這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
本文標(biāo)題:如何用Shell腳本統(tǒng)計(jì)當(dāng)前目錄下目錄和文件的數(shù)量-創(chuàng)新互聯(lián)
路徑分享:http://aaarwkj.com/article16/ihedg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、小程序開發(fā)、網(wǎng)站排名、App開發(fā)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容