Shell:查找应该忽略空格

发布于 2024-12-09 01:52:40 字数 194 浏览 0 评论 0原文

我有一个 shellscript,它是从 NSTask 运行的,是动态构建的。除了一件事之外,一切正常:

如果文件名包含空格,则 find 命令将忽略它。我这样使用它:'find -iname *.xxx'。

如果文件名类似于“aaa bbb.xxx”,则表示未找到该文件。

任何帮助表示赞赏。

问候, 马库斯

I've a shellscript, that I run from an NSTask, that I build dynamically. Everything works fine besides one thing:

If the filename contains a blank, it's ignored by the find-command. I use it like that: 'find -iname *.xxx'.

If the filename looks something like 'aaa bbb.xxx', than it's not found.

Any help appreciated.

Regards,
Marcus

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦巷 2024-12-16 01:52:40

find 不关心文件名中是否有空格。那不是你的问题。

如果您实际上按照问题中键入的方式调用它(find -iname *.xxx),那么问题是您需要引用文件名模式以保护它免受 shell 扩展:查找-iname'*.xxx'。注意模式周围的引号;它们是必不可少的。

否则,问题很可能出在您处理返回的文件名的方式上。例如,这是行不通的:

for f in `find -iname '*.xxx'`; do
   echo "file: $f"
done

你会看到反引号运算符实际上在空格上分割(或者,实际上是 $IFS),并且你得到两个“文件”: 'aaa' 和 ' bbb.xxx'。

find does not care if your file names have spaces in them. That's not your problem.

If you're actually calling it as typed in your question (find -iname *.xxx), then the problem is that you need to quote the file name pattern to protect it from shell expansion: find -iname '*.xxx'. Note the quotes around the pattern; they're essential.

Otherwise, the problem is very likely in how you're handling the file names you're getting back. For example, this won't work:

for f in `find -iname '*.xxx'`; do
   echo "file: $f"
done

you'll see that the backtick operator actually splits on spaces (or, $IFS, actually) and you get back two "files": 'aaa' and 'bbb.xxx'.

青瓷清茶倾城歌 2024-12-16 01:52:40

像这样逃避模式:

find -iname '*.xxx' -print

如果有帮助,请考虑+1。谢谢

Escape the Pattern like this:

find -iname '*.xxx' -print

please consider +1 if it helped. thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文