跪求 用Shell 脚本 实现 统计test目录的各文件的行数 并分类

发布网友 发布时间:2022-04-23 02:28

我来回答

3个回答

热心网友 时间:2023-10-11 18:30

#!/bin/bash
# count the line of the file.
MYDIR="/root/test"
DIRLIST=`ls ${MYDIR}`
SF=()
MF=()
LF=()
for i in ${DIRLIST}
do
LINE=`cat ${MYDIR}/$i | wc -l`
if ((${LINE}<10))
then
SF=(${SF[*]} $i)
elif ((${LINE}>=10)) && ((${LINE}<=100))
then
MF=(${MF[*]} $i)
elif ((${LINE}>100))
then
LF=(${LF[*]} $i)
fi
done
echo Small files: ${SF[*]}
echo Medium files: ${MF[*]}
echo Large files: ${LF[*]}

已测试正确并无错误,把你要测试的目录的路径改下即可

热心网友 时间:2023-10-11 18:30

#!/bin/bash

wc -l $1/* 2>/dev/null | \
awk 'BEGIN{
small=""
medium=""
large=""
}
{
if($1 < 10)
small = small (small=="" ? "" : " ") $2
else if($1 < 100)
medium = medium (medium=="" ? "" : " ") $2
else
large = large (large=="" ? "" : " ") $2
}
END{
print "small files: " small
print "medium files: " medium
print "large files: " large
}'

把上述存为一个文件,比如叫xx33, chmod 755 xx33, 如果要查看路径/tmp/ss:

./xx33 /tmp/ss

热心网友 时间:2023-10-11 18:31

#!/bin/sh
small_files=""
medium_files=""
large_files=""
for i in test/*; do
lines=`wc -l "$i" | cut -f 1 -d ' '`
filename=`basename "$i"`
if [ $lines -lt 10 ]; then
small_files="$small_files $filename"
elif [ $lines -lt 100 ]; then
medium_files="$medium_files $filename"
else
large_files="$large_files $filename"
fi
done
echo "Small files: $small_files"
echo "Medium files: $medium_files"
echo "Large files: $large_files"

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com