gpt4 book ai didi

shell - ./*/是可移植的吗?

转载 作者:行者123 更新时间:2023-12-04 14:53:40 26 4
gpt4 key购买 nike

我经常在for循环中使用./*/

for d in ./*/; do
: # do something with dirs
done

匹配当前工作目录中的所有非隐藏目录,但我不确定这是否是一种可移植的方式。我的系统上安装了 bash、dash 和 ksh,它适用于所有系统,但自 POSIX spec什么也没说(或者它含蓄地说,我错过了)我想我不能依赖它。我还检查了POSIX bug reports , 但无济于事,那里也没有提到它。

它的行为实现或文件系统依赖吗?我在这里错过了什么吗?我怎么知道它是否便携?

最佳答案

简短回答:是

长答案:

POSIX 标准(来自 opengroup)指出 / 只会匹配扩展文件名中的斜线。由于 Unix/Linux 不允许在文件名中使用 /,我相信这在 Unix/Linux 系统上是一个安全的假设。

从下面的粗体文本来看,似乎即使对于允许在文件名中使用 / 的系统,POSIX 标准也要求 / 不会与此类匹配文件。

在 Windows 上,文件名中似乎不允许使用 /,但我不是 Windows 专家。

来自 Shell Programming Language § Patterns Used for Filename Expansion :

The slash character in a pathname shall be explicitly matched by using one or more slashes in the pattern; it shall neither be matched by the asterisk or question-mark special characters nor by a bracket expression. Slashes in the pattern shall be identified before bracket expressions; thus, a slash cannot be included in a pattern bracket expression used for filename expansion. ...

附加说明 - 阐明路径名:

路径名 在 4.13 中定义,在 General Concepts § Pathname Resolution 中明确引用带有尾部斜杠的路径名.

A pathname that contains at least one non-<slash> character and that ends with one or more trailing <slash> characters shall not be resolved successfully unless the last pathname component before the trailing <slash> characters names an existing directory or a directory entry that is to be created for a directory immediately after the pathname is resolved. Interfaces using pathname resolution may specify additional constraints when a pathname that does not name an existing directory contains at least one non-<slash> character and contains one or more trailing <slash> characters.

关于shell - ./*/是可移植的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58176457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com