Bash循环查找结果并获取找到的项目类型

问题描述 投票:1回答:1

我有一些代码:

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'

我不知道出了什么问题。有人知道吗?

bash find
1个回答
1
投票

-l不测试符号链接。使用-h-L

您可以使用-L(或-h,它相当)与[[...]][...]

  • [[ -L $item ]]的优点是你可以不引用$item
  • 虽然它需要$item的双引号,但[ -L "$item" ]的优势在于它是POSIX,因此非常便携。
© www.soinside.com 2019 - 2024. All rights reserved.