#for d in $DIRCOUNT
#do
# DIRCOUNT=$[$DIRCOUNT+1]
#done
#for f in $FILECOUNT
#do
# FILECOUNT=$[$FILECOUNT+1]
#done
#for r in $READ
#do
# READ=$[$READ+1]
#done
#for w in $WRITE
#do
# WRITE=$[$WRITE+1]
#done
#for e in $EXECUTABLE
#do
# EXECUTE=$[$EXECUTE+1]
#done
for item in $LOCATION/* $LOCATION/.*
do
if [ -f "$item" ]
then
FILECOUNT=$[$FILECOUNT+1]
elif [ -d "$item" ]
then
DIRCOUNT=$[$DIRCOUNT+1]
elif [ -r "$item" ]
then
READ=$[$READ+1]
elif [ -w "$item" ]
then
WRITE=$[$WRITE+1]
elif [ -e "$item" ]
then
EXECUTE=$[$EXECUTE+1]
fi
done
echo "Number of directories: " $DIRCOUNT
echo "Number of files: " $FILECOUNT
echo "Number of readable: " $READ
echo "Number of writble: " $WRITE
echo "Number of executable: " $EXECUTE
我想找出目录和文件的数量及其类型。我是新手,我不知道如何处理读写和执行。我实际上不知道elif部分发生了什么,有人可以向我解释一下我做了什么,我该怎么做?
答案 0 :(得分:1)
w.r.t语法有几种选择:
test -f "$item" && : $((FILECOUNT++))
test -d "$item" && DIRCOUNT=$( expr $DIRCOUNT + 1 )
是我喜欢的两个。这里的else / if链可能不合适,因为你永远不会看到READ,WRITE或EXECUTE的增量,而且许多文件都有几个属性。
答案 1 :(得分:0)
您的if-elsif
链的最大问题是条目的类型(例如,目录,文件)与其模式正交(例如,读取,写入,执行)。您应该使用一个if
语句链来检测条目类型,使用第二个语句来处理模式。
for entry in $LOCATION/* $LOCATION/.*
do
if [ -d "$entry" ]; then
DIRCOUNT=$((DIRCOUNT + 1))
elif [ -f "$entry" ]; then
FILECOUNT=$((FILECOUNT + 1))
elif [ -L "$entry" ]; then
LINKCOUNT=$((LINKCOUNT + 1))
elif [ -c "$entry" -o -b "$entry" ]; then
DEVCOUNT=$((DEVCOUNT + 1))
else
UNKNOWNCOUNT=$((UNKNOWNCOUNT + 1))
fi
if [ -r "$entry" ]; then
READABLECOUNT=$((READABLECOUNT + 1))
elif [ -w "$entry" ]; then
WRITABLECOUNT=$((WRITEABLECOUNT + 1))
elif [ -x "$entry" ]; then
ACTIONABLECOUNT=$((ACTIONABLECOUNT + 1))
fi
end
有几点需要注意: