执行名称中带 * 的 ls 命令
我想对名为 /var/local/tmp/foo* 的文件执行 ls 命令。我一直在尝试使用,
%x[ls "#{path]"]
但会返回:
ls: /var/local/tmp/foo*: No such file or directory
请注意 %x[ls '/var/local/tmp/foo*'] 也会返回上述错误。
如果我执行相同的命令,但如下所示:
%x[ls /var/local/tmp/foo*]
它可以正常工作。
我猜测将目录路径放入字符串中会导致 ls 将其视为字面名为“/var/local/tmp/foo*”的文件。
有什么想法可以解决这个问题吗?我想使用一个保存该字符串的变量,因此直接放入 /var/local/tmp/foo* 不是一个选择。
I would like to perform an ls command on a file named like /var/local/tmp/foo*. I have been trying to use
%x[ls "#{path]"]
but this returns:
ls: /var/local/tmp/foo*: No such file or directory
Note that %x[ls '/var/local/tmp/foo*'] also returns above error.
If I do the same command, but as follows:
%x[ls /var/local/tmp/foo*]
it works correctly.
I am guessing that putting the directory path into a string is causing ls to treat it as a file literally named "/var/local/tmp/foo*".
Any ideas how to get around this? I would like to use a variable that holds this string, so just putting in /var/local/tmp/foo* directly is not an option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像
"/path/to/*"
之类的东西被称为 glob,它们实际上是由 shell 扩展的,而不是由 这样的命令扩展的代码>ls。虽然您可以运行 shell 来让它为您扩展 glob,但使用内置的Dir
类。这将返回匹配文件名的数组。
Something like
"/path/to/*"
is called a glob, and they are actually expanded by the shell, not by commands likels
. While you could run the shell to have it expand the glob for you, it is easier to just use the built-inDir
class.This returns an array of matching file names.