我有一些代码:
for item in $MYPATH
do
if [[ -f $item ]] ; then #finds all files
FILECOUNT=$[$FILECOUNT+1]
elif [[ -d $item ]] ; then #finds all folders
DIRCOUNT=$[$DIRCOUNT+1]
elif [[ -l $item ]] ; then #supposed to find all symlinks
SYMCOUNT=$[$SYMCOUNT+1]
fi
done
当我只使用文件和目录类型运行它时,它工作正常,并给我我的计数。但是当我添加-l
if语句来计算符号链接时,它会抛出以下错误:
conditional binary operator expected
syntax error near `$item'
`elif [[ -l $item ]] ; then'
我不知道出了什么问题。有人知道吗?
-l
不测试符号链接。使用-h
或-L
。
您可以使用-L
(或-h
,它相当)与[[...]]
或[...]
。
[[ -L $item ]]
的优点是你可以不引用$item
。$item
的双引号,但[ -L "$item" ]
的优势在于它是POSIX,因此非常便携。