glob 中文man页面

系统
很久以前 在 UNIX V6 版 中 有一个 程序 /etc/glob 用来 展开 通配符模板. 不久以后 它 成为 shell 内建功能. 现在 人们 开发了 类似的 库函数 glob(3), 让 用户程序 实现 同样的 功能.

NAME

glob - 形成路径名称

描述 (DESCRIPTION)

很久以前 在 UNIX V6 版 中 有一个 程序 /etc/glob 用来 展开 通配符模板. 不久以后 它 成为 shell 内建功能. 现在 人们 开发了 类似的 库函数 glob(3), 让 用户程序 实现 同样的 功能.

此 规则 遵循 (POSIX 1003.2, 3.13).

通配符匹配 (WILDCARD MATCHING)

包含 '?', '*' 或 '[' 字符的 字符串 称为 通配符模板(wildcard pattern). 形成路径名(globbing) 指 一种 操作, 把 通配符模板 展开为 匹配 该串的 路径名. 匹配 定义为:

不在 方括弧中 的 '?' 匹配 任意 单个 字符.

不在 方括弧中 的 '*' 匹配 任意 字符串, 包括 空串.

字符集 (Character classes)

对于 表达式 `[...]', 如果 在 ***个 '['符 后面 出现的 ***个 字符 不是 '!', 则 该 表达式 匹配 任意 一个 在 `[...]'内 出现的 字符. 方括弧内 不能 有 空串, 因此 ']' 可以 作为 ***个 字符 出现在 方括弧内. (像 这样, '[][!]' 匹配 下列三个 字符 中的 任意 一个, '[', ']' 和 '!'.)

范围集 (Ranges)

字符集 有一个 特例: 用 '-' 分开的 两个 字符 表示 一个 范围集. (像 这样, `[A-Fa-f0-9]' 等于 `[ABCDEFabcdef0123456789]'.) 把 '-' 放到 方括弧内 的 开头 或 *** 可以 获得 它的 本意. (像 这样, `[]-]' 匹配 ']'和'-' 中 任意 一个. 而 `[--/]' 匹配 `-', `.' 和`/'中任意 一个.)

补集 (Complementation)

表达式 '[!...]' 表示 一个 字符, 该 字符 不匹配 方括弧内 去掉 开头 '!' 后的 表达式. (像 这样, `[!]a-]' 匹配 除了 ']', 'a' 和 '-' 的 任意 一个 字符.)

要 去掉 '?', '*' 和 '[' 的 特殊 含义, 可以 通过 前面 加 一个 反斜杠; 或者 在 shell 命令行 中, 通过 引号 来 引用 这些 字符. 在 方括弧内 这些 字符 显露出 本意, 所以, '[[?*\]' 匹配 这 四个字符中 的 一个: '[', '?', '*', '\'.

路径名 (PATHNAME)

形成路径名 功能 应用于 路径 中 的 每一个 成员部分. 路径 中 的 '/' 不能 被通配符 '?' 或 '*', 或 范围集 如 '[.-0]' 匹配. 范围集 不能 直接 包含 '/', 否则 导致 语法错误.

如果 待匹配的 文件名 以'.'开头, 那么 这个 '.' 字符 必须 直接 给出. (比如说, 用 'tar c .' 会 更好.)

空列表 (EMPTY LISTS)

上述的 简单优雅 规则, 把 通配符模板 展开为 匹配的 路径名, 来源于 最初的 UNIX 定义. 它 允许 展开出 空串, 例如


 

        xv -wait 0 *.gif *.jpg

这里 可能 没有 *.gif 文件 (而且 不算 错误).

然而, POSIX 要求 句法 错误 或 路径名 列表 为 空 时, 保留 通配符模板 不变. (译注: 即 不展开.)

bash 中 可以 通过 设置 allow_null_glob_expansion=true 把 它 强置为 传统的 风格. (其他 地方 也有 类似的 问题, 例如, 老式的 语句 是
 

        rm `find . -name "*~"`

新的 写法 为
 

        rm -f nosuchfile `find . -name "*~"`

以 避免 由于 空参数调用 rm 而 产生 错误信息.)

注意 (NOTES)

正规表达式 (Regular expressions)

注意, 通配符模板 不是 正规表达式, 尽管 它们 有点象. 首先, 它 匹配 文件名, 而 不是 正文; 其次, 规则 不一样, 例如 正规表达式 里 的 '*' 代表 零个或多个 前面内容的 重复.

正规表达式 的 方括弧表达式 用 '^' 引导 取反操作, (而不是 '[!...]'). POSIX 声明, 在 通配符模板 中, '[^...]' 未做 定义.

字符集 和 国际化 (Character classes and Internationalization )

当然, 范围集 最初 指 ASCII的 范围, 因此 '[ -%]' 意思是 '[ !"#$%]',

一些 UNIX实现 把 这个 归纳为: 范围 X-Y 指 X的编码 到 Y的编码 之间的编码字符. 可是, 这 要求 用户 知道 他们 本地系统的 字符编码, 此外, 如果本地的 字母表顺序 和 字符集顺序 不对应, 那 就 更不方便了.

因此, POSIX 对 通配符模板 和 正规表达式 的 方括弧表达法 作了 重大扩展, 上面 我们 知道了 方括弧表达式 中 的 三个 类型, 它们是 (i) 取补集 (ii) 直接列出的 单个字符 和 (iii) 范围集.

POSIX 对 范围集 在 国际化 方面 作了 更有力的 说明, 并且 增加了 三个 类型:

(iii) 范围 X-Y 由 X 和 Y 之间 所有的字符 组成 (包括X和Y), X 和 Y 的 当前编码序列 由 当前场合的 LC_COLLATE 分类定义.

(iv) 命名字符集, 象
 

[:alnum:]  [:alpha:]  [:blank:]  [:cntrl:]
[:digit:]  [:graph:]  [:lower:]  [:print:]
[:punct:]  [:space:]  [:upper:]  [:xdigit:]

因此 可以 用 '[[:lower:]]' 代替 '[a-z]', 它 在 丹麦语 里 同样 有效, 虽然 丹麦的 字母表 里 'z' 后面 还有 三个 字母. 这些 字符集 由 当前场合的 LC_CTYPE 分类定义.

(v) 符号对映, 象 '[.ch.]' 或 '[.a-acute.]', 在 '[.' 和 '.]' 之间的 字符串 是 定义 在 当前场合的 对映元素. 注意 这 可以 是 多字符元素.

(vi) 等类表达式, 象 '[=a=]', 在 '[=' 和 '=]' 之间的 字符串 是 任意 等类 中 的 对映元素, 它 定义在 当前场合. 例如, '[[=a=]]' 可以 等同于 `[a徉溻]' (警告: 这里 有 Latin-1 字符), 也就是 `[a[.a-acute.][.a-grave.][.a-umlaut.][.a-circumflex.]]'.

SEE ALSO

sh(1), glob(3), fnmatch(3), locale(7), regex(7)

#p#

NAME

glob - Globbing pathnames  

DESCRIPTION

Long ago, in Unix V6, there was a program /etc/glob that would expand wildcard patterns. Soon afterwards this became a shell built-in.

These days there is also a library routine glob(3) that will perform this function for a user program.

The rules are as follows (POSIX 1003.2, 3.13).  

WILDCARD MATCHING

A string is a wildcard pattern if it contains one of the characters `?', `*' or `['. Globbing is the operation that expands a wildcard pattern into the list of pathnames matching the pattern. Matching is defined by:

A `?' (not between brackets) matches any single character.

A `*' (not between brackets) matches any string, including the empty string.

Character classes

An expression `[...]' where the first character after the leading `[' is not an `!' matches a single character, namely any of the characters enclosed by the brackets. The string enclosed by the brackets cannot be empty; therefore `]' can be allowed between the brackets, provided that it is the first character. (Thus, `[][!]' matches the three characters `[', `]' and `!'.)

Ranges

There is one special convention: two characters separated by `-' denote a range. (Thus, `[A-Fa-f0-9]' is equivalent to `[ABCDEFabcdef0123456789]'.) One may include `-' in its literal meaning by making it the first or last character between the brackets. (Thus, `[]-]' matches just the two characters `]' and `-', and `[--0]' matches the three characters `-', `.', `0', since `/' cannot be matched.)

Complementation

An expression `[!...]' matches a single character, namely any character that is not matched by the expression obtained by removing the first `!' from it. (Thus, `[!]a-]' matches any single character except `]', `a' and `-'.)

One can remove the special meaning of `?', `*' and `[' by preceding them by a backslash, or, in case this is part of a shell command line, enclosing them in quotes. Between brackets these characters stand for themselves. Thus, `[[?*\]' matches the four characters `[', `?', `*' and `\'.

PATHNAMES

Globbing is applied on each of the components of a pathname separately. A `/' in a pathname cannot be matched by a `?' or `*' wildcard, or by a range like `[.-0]'. A range cannot contain an explicit `/' character; this would lead to a syntax error.

If a filename starts with a `.', this character must be matched explicitly. (Thus, `rm *' will not remove .profile, and `tar c *' will not archive all your files; `tar c .' is better.)

EMPTY LISTS

The nice and simple rule given above: `expand a wildcard pattern into the list of matching pathnames' was the original Unix definition. It allowed one to have patterns that expand into an empty list, as in
 

        xv -wait 0 *.gif *.jpg

where perhaps no *.gif files are present (and this is not an error). However, POSIX requires that a wildcard pattern is left unchanged when it is syntactically incorrect, or the list of matching pathnames is empty. With bash one can force the classical behaviour by setting allow_null_glob_expansion=true.

(Similar problems occur elsewhere. E.g., where old scripts have
 

        rm `find . -name "*~"`

new scripts require
 

        rm -f nosuchfile `find . -name "*~"`

to avoid error messages from rm called with an empty argument list.)

NOTES

Regular expressions

Note that wildcard patterns are not regular expressions, although they are a bit similar. First of all, they match filenames, rather than text, and secondly, the conventions are not the same: e.g., in a regular expression `*' means zero or more copies of the preceding thing.

Now that regular expressions have bracket expressions where the negation is indicated by a `^', POSIX has declared the effect of a wildcard pattern `[^...]' to be undefined.

Character classes and Internationalization

Of course ranges were originally meant to be ASCII ranges, so that `[ -%]' stands for `[ !"#$%]' and `[a-z]' stands for "any lowercase letter". Some Unix implementations generalized this so that a range X-Y stands for the set of characters with code between the codes for X and for Y. However, this requires the user to know the character coding in use on the local system, and moreover, is not convenient if the collating sequence for the local alphabet differs from the ordering of the character codes. Therefore, POSIX extended the bracket notation greatly, both for wildcard patterns and for regular expressions. In the above we saw three types of items that can occur in a bracket expression: namely (i) the negation, (ii) explicit single characters, and (iii) ranges. POSIX specifies ranges in an internationally more useful way and adds three more types:

(iii) Ranges X-Y comprise all characters that fall between X and Y (inclusive) in the currect collating sequence as defined by the LC_COLLATE category in the current locale.

(iv) Named character classes, like
 

[:alnum:]  [:alpha:]  [:blank:]  [:cntrl:]
[:digit:]  [:graph:]  [:lower:]  [:print:]
[:punct:]  [:space:]  [:upper:]  [:xdigit:]

so that one can say `[[:lower:]]' instead of `[a-z]', and have things work in Denmark, too, where there are three letters past `z' in the alphabet. These character classes are defined by the LC_CTYPE category in the current locale.

(v) Collating symbols, like `[.ch.]' or `[.a-acute.]', where the string between `[.' and `.]' is a collating element defined for the current locale. Note that this may be a multi-character element.

(vi) Equivalence class expressions, like `[=a=]', where the string between `[=' and `=]' is any collating element from its equivalence class, as defined for the current locale. For example, `[[=a=]]' might be equivalent to `[a??? ????]' (warning: Latin-1 here), that is, to `[a[.a-acute.][.a-grave.][.a-umlaut.][.a-circumflex.]]'.

SEE ALSO

sh(1), glob(3), fnmatch(3), locale(7), regex(7)

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-25 15:09:38

clearerr中文man

2011-08-25 15:19:39

dirname中文man

2011-08-25 15:21:53

execl中文man

2011-08-25 16:28:50

fread中文man

2011-08-25 16:52:54

getchar中文man

2011-08-25 17:27:58

rewind中文man

2011-08-25 18:41:31

vprintf中文man

2011-08-23 10:03:40

useradd中文man

2011-08-23 10:29:02

chpasswd中文man

2011-08-23 10:34:22

convertquot中文man

2011-08-23 15:39:34

rpmbuild中文man

2011-08-23 18:00:21

LDP中文man

2011-08-25 09:40:49

UPDATE中文man

2011-08-24 15:11:15

explain中文man

2011-08-24 15:29:06

grant中文man

2011-08-24 15:48:38

INSERT中文man
点赞
收藏

51CTO技术栈公众号