在 shell 中实现通配符扩展
我正在尝试创建自定义 shell 作为练习,并希望实现通配符扩展。像 bash 这样的 shell 究竟是如何执行扩展的?我的意思是所有步骤都涉及什么?
据我了解,shell 在当前目录中查找文件名,并将包含“*”的参数替换为应该匹配的文件名。这是正确的吗?除了“*”之外,shell 还应该执行哪些其他通配符扩展
I am trying to create custom shell as an exercise and wanted to implement wildcard expansion. How exactly do shells like bash perform the expansion? I mean what all steps are involved?
As I understand, the shell looks for file names in the current directory and replaces the argument which contains the '*' with the filenames which should match. Is this correct? What other wildcard expansions should a shell do other than a '*'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
POSIX 规范描述了 POSIX 兼容 shell 应该进行的单词扩展支持。您可以使用
glob
或wordexp
POSIX 函数来执行这些扩展(glob
仅支持wordexp< 扩展的一小部分子集) /code> 支持)。
The POSIX specification describes the word expansions that POSIX-compliant shells should support. You can use the
glob
orwordexp
POSIX functions to perform these expansions (glob
supports only a small subset of the expansions thatwordexp
supports).Bourne shell [原来的 sh] 支持
*
、?
和[range]
扩展。 bash 还支持**
Bourne shell [original sh] supports
*
,?
, and[range]
expansion. bash also supports**
从技术上讲,通配符扩展与模式匹配概念密切相关。大致而言,涉及的步骤包括:
至于各种字符的全部范围,请查看特定 shell 实现的文档(例如,对于 bash, zsh, ETC)。这些内容中的大多数直接映射到类似正则表达式的机制的一个或多个特征。
Technically, wildcard expansion is closely related to a pattern matching concept. Very roughly, steps involved include:
As for full range of various characters, take a look at documentation for particular shell implementations (for example, for bash, zsh, etc). Most of these stuff map directly into one or several features of regular expression-like mechanism.
您可以在标记提示命令时执行通配符扩展。使用 glob(3) 库执行通配符扩展。通过在 glob 函数中设置不同的标志,可以执行各种类型的扩展。 glob(3) 文档
不同类型通配符扩展的参考- 这里
You can perform wildcard expansion at the time of tokenizing the command given to prompt. Use glob(3) library to perform wildcard expansion. By setting different flags in the glob function, various types of expansion can be performed. glob(3) documentation
Reference for different types of wildcard expansions - Here