没有找到关于shell开发的板块,所以写在这里。
我的原题是统计一个目录下的文件个数和目录个数。我写的程序如下:
#!/bin/bash
fsum=0
dsum=0
# $1是我要统计的目录,由调用时作为参数传递
for var in $(ls $1)
do
if [ -f $var ];then
fsum=$(($fsum+1))
elif [ -d $var ];then
dsum=$(($dsum+1))
else
:
fi
done
echo "the total file number is $fsum"
echo "the total directory number is $dsum"
exit 0
很明显了,$var只是个字符串,当然没有文件或者目录的属性了,你用/root,因为/表示了根目录,OK,他才可以判断到属性啊,下面是我改的,可以参考一下,如果要用到更方便,还要改的,呵呵,我也是初学者:)
[code:1]
#!/bin/bash
fsum=0
dsum=0
for var in $(ls $1)
do
if [ -f './'$1'/'$var ];then
fsum=$(($fsum+1))
elif [ -d './'$1'/'$var ];then
dsum=$(($dsum+1))
else
:
fi
done
echo "the total file number is $fsum"
echo "the total directory number is $dsum"
exit 0
[/code:1]