为什么我会收到二进制操作员的预期错误
我正在尝试编写一个shell脚本以检查是否存在使用if语句以.txt结尾的文件。
I'm trying to write a shell script to check if there's a file existing that ends with .txt using an if statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在单个支架条件中,所有
审核构建体对所给出的参数的 number 进行:
-f
期望完全遵循一个参数,一个文件名。显然,您的*。TXT
模式匹配多个文件。如果您的外壳是bash,您可以做到
或更便宜:
我终于得到了您的观点。我一直给您一些不完整的建议。这就是您需要的:
原因:如果有没有匹配模式的文件,则Shell 将Patten作为平单字符串。在循环的第一次迭代中,我们具有
f =“*。txt”
和mv
以“未找到文件”响应。我习惯于使用处理此边缘情况的
nullglob
选项。Within single bracket conditionals, all of the Shell Expansions will occur, particularly in this case Filename expansion.
The condional construct acts upon the number of arguments it's given:
-f
expects exactly one argument to follow it, a filename. Apparently your*.txt
pattern matches more than one file.If your shell is bash, you can do
or, more portably:
I finally get your perspective now. I've been giving you some incomplete advice. This is what you need:
The reason: if there are no files matching the pattern then the shell leaves the patten as a plain string. On the first iteration of the loop, we have
f="*.txt"
andmv
responds with "file not found".I'm used to working in bash with the
nullglob
option that handles this edge case.