我已经看到很多关于这个主题的答案,但我不想这样做
find
。我写过这个但不起作用的东西:
function CountEx()
{
count=0
for file in `ls $1`
do
echo "file is $file"
if [ -x $file ]
then
count=`expr $count + 1`
fi
done
echo "The number of executable files in this dir is: $count"
}
while getopts x:d:c:h opt
do
case $opt in
x)CountEx $OPTARG;;
d)CountDir $OPTARG;;
c)Comp $OPTARG;;
h)help;;
*)echo "Please Use The -h Option to see help"
break;;
esac
done
我正在使用以下脚本:
yaser.sh -x './..../...../.....'
shell运行它,然后输出:
The number of executable files in this dir is: 0
当此目录中有许多可执行文件时。
答案 0 :(得分:0)
如果你的目标是计算目录,那么有很多选择。
find
方式,您说您不想要:
CountDir() {
if [[ ! -d "$1" ]]; then
echo "ERROR: $1 is not a directory." >&2
return 1
fi
printf "Total: %d\n" $(find "$1" -depth 1 -type d | wc -l)
}
for
方式,类似于您的示例:
CountDir() {
if [[ ! -d "$1" ]]; then
echo "ERROR: $1 is not a directory." >&2
return 1
fi
count=0
for dir in "$1"/*; do
if [[ -d "$dir" ]]; then
((count++))
fi
done
echo "Total: $count"
}
set
方式,完全跳过循环。
CountDir() {
if [[ ! -d "$1" ]]; then
echo "ERROR: $1 is not a directory." >&2
return 1
fi
set -- "$1"/*/
echo "Total: $#"
}
答案 1 :(得分:0)
计算可执行文件的数量(如标题所示)
count=0
for file in yourdir/*; do
if [ -x $file ]; then
count=$((count+1));
fi;
done;
echo "total ${count}"
要计算文件夹,只需使用-x
-d
测试