linux:访问包含空格的目录
在脚本中,我有这一行
#!/bin/sh
log="${var}logs/console logs/since2_%m-%d-%Y.log" # <-- console logs has a space
如何访问该文件?
添加引号,例如:
log="${var}logs/"console logs"/since2_%m-%d-%Y.log"
取消其周围的引号,转义引号使其尝试查找包含字符“的文件
In a script, I have this line
#!/bin/sh
log="${var}logs/console logs/since2_%m-%d-%Y.log" # <-- console logs has a space
how can I access this file?
putting quotes like:
log="${var}logs/"console logs"/since2_%m-%d-%Y.log"
cancels out the quotes around it, and escaping the quotes makes it try to find a file containing the character "
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您遇到的问题可能是您使用 $log 的地方,您可能应该使用“$log”来保留空格。
The trouble you're having is probably where you use $log, you should probably be using "$log" to preserve the spaces.
问题不在于问题中引用的内容。这是一个有效的示例脚本。除了定义之外,请注意 $log 的用法周围的引号。如果您需要进一步的帮助,请发布完整的脚本或最小的工作子集,人们可以运行它们来重现问题。
The problem is not what is quoted in the question. Here is an example script which works. Note the quotes around the USAGE of $log in addition to the definition. If you want further help, post the complete script or a minimal working subset which people can run to reproduce the problem.
变量 $IFS 保存字段分隔符,默认情况下为空格,因此请尝试使用
the variable $IFS holds the field separator, which by default is space, so try with
如果您打算在该文件名中包含今天的日期:
我建议您使用
%Y-%m-%d
因为它可以按时间顺序和词汇顺序排序。If you intend to have today's date in that filename:
I'd recommend you use
%Y-%m-%d
as that sorts both cronologically and lexically.我认为
log="${var}logs/console\logs/since2_%m-%d-%Y.log"
应该可以。尝试一次这个想法是逃离[空间]
I think
log="${var}logs/console\ logs/since2_%m-%d-%Y.log"
should work. Try onceThe idea is to escape the [SPACE]