如何通过检查换行符确保只返回一个文件
检查字符串是否包含换行符的最简单方法是什么?
例如,在
FILE=$(find . -name "pattern_*.sh")
我想检查换行符以确保只有一个文件匹配之后。
What is the simplest way to check if a string contains newline?
For example, after
FILE=$(find . -name "pattern_*.sh")
I'd like to check for newline to ensure only one file matched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用模式匹配:
You can use pattern matching:
如果
$str
包含新行,您可以通过以下方式检查:If
$str
contains new line you can check it by,参考您的示例:请注意,文件名也可以包含换行符。
计算文件数量的安全方法是
这避免打印文件名并仅打印换行符。
To refer to your example: Note that filenames could contain newlines, too.
A safe way to count files would be
This avoids printing the filename and prints only a newline instead.