如何在 shell 脚本中使用“查找”从当前目录进行搜索?

Cio*_*res 1 scripts directory find

我希望程序看起来像这样:

read a
if[ find . name $a ]; then
  echo "You found the file"
else "You haven t found the file"
fi
Run Code Online (Sandbox Code Playgroud)

mur*_*uru 5

无论是否找到任何东西,find总是返回 true。您可以使用grep来确定是否find找到了一些东西:

read -r a
if find . -maxdepth 1 -name "$a" -print -quit | grep -q . 
then
  echo "You found the file"
else 
  echo "You haven't found the file"
fi
Run Code Online (Sandbox Code Playgroud)

-print -quit正如 Eliah 所指出的那样,在第一场比赛 ( )后退出应该会提高性能。用于-maxdepth 1将结果限制到当前目录 - 但这样find就显得过分了。