Shell:查找应该忽略空格
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
find
不关心文件名中是否有空格。那不是你的问题。如果您实际上按照问题中键入的方式调用它(
find -iname *.xxx
),那么问题是您需要引用文件名模式以保护它免受 shell 扩展:查找-iname'*.xxx'
。注意模式周围的引号;它们是必不可少的。否则,问题很可能出在您处理返回的文件名的方式上。例如,这是行不通的:
你会看到反引号运算符实际上在空格上分割(或者,实际上是
$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:
you'll see that the backtick operator actually splits on spaces (or,
$IFS
, actually) and you get back two "files": 'aaa' and 'bbb.xxx'.像这样逃避模式:
如果有帮助,请考虑+1。谢谢
Escape the Pattern like this:
please consider +1 if it helped. thanks