- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一个变量POSIXLY_CORRECT在 Bash 中
POSIXLY_CORRECT
If this variable is in the environment when Bash starts, the shell enters POSIX mode (see Bash POSIX Mode) before reading the startup files, as if the --posix invocation option had been supplied. If it is set while the shell is running, Bash enables POSIX mode, as if the command
set -o posix
had been executed.
有人告诉我 grep
的一些选项不是 POSIX,所以我在 The Open Group Base Specifications Issue 6 for grep
中确认了.所以我检查了 GNU grep manual并发现:
grep comes with a rich set of options: some from POSIX and some being GNU extensions. Long option names are always a GNU extension, even for options that are from POSIX specifications. Options that are specified by POSIX, under their short names, are explicitly marked as such to facilitate POSIX-portable programming. A few option names are provided for compatibility with older or more exotic implementations.
它还提到:
The behavior of grep is affected by the following environment variables.
POSIXLY_CORRECT
If set, grep behaves as POSIX requires; otherwise, grep behaves more like other GNU programs. POSIX requires that options that follow file names must be treated as file names; by default, such options are permuted to the front of the operand list and are treated as options. Also, POSIXLY_CORRECT disables special handling of an invalid bracket expression. See invalid-bracket-expr.
使用部分 长选项名称始终是 GNU 扩展,即使对于来自 POSIX 规范的选项也是如此 我说:让我们尝试使用变量 POSIXLY_CORRECT 来对抗它。
所以我确实尝试了一些非 POSIX 的东西:
$ echo "HELLO" | grep --ignore-case 'hello'
HELLO
但令我惊讶的是它也可以设置它:
$ echo "HELLO" | POSIXLY_CORRECT=1 grep --ignore-case 'hello'
HELLO
我做错了什么? POSIXLY_CORRECT 集不应该让 grep
无法识别长选项名称吗?
如果使用非 POSIX 的选项(例如 -C
),也会发生同样的情况:
$ POSIXLY_CORRECT=1 grep -C 2 '2' <<< "1
2
3"
1
2
3
以及之前执行所有相同的运行 set -o posix
。
最佳答案
来自 GNU grep
手册:
POSIXLY_CORRECT
If set,
grep
behaves as POSIX requires; otherwise,grep
behaves more like other GNU programs. POSIX requires that options that follow file names must be treated as file names; by default, such options are permuted to the front of the operand list and are treated as options. Also, POSIX requires that unrecognized options be diagnosed as "illegal", but since they are not really against the law the default is to diagnose them as "invalid".POSIXLY_CORRECT
also disables_N_GNU_nonoption_argv_flags_
, described below.
这意味着在环境中设置 POSIXLY_CORRECT
对 GNU grep
的唯一作用是不允许重新排列文件名后出现的选项,以便放在前面。它不会使其不采用非 POSIX 命令行标志。
那么让我们试试看:
$ ggrep "hello" myfile -v
$ env POSIXLY_CORRECT=1 ggrep "hello" myfile -v
ggrep: -v: No such file or directory
(GNU grep
在我的 BSD 系统上被称为 ggrep
)
手册中关于“无法识别的选项”的部分是 GNU grep
默认所做的,即 -g
标志将被诊断为“无效” POSIXLY_CORRECT
和没有。因为例如--ignore-case
是一个有效选项(尽管不是 POSIX),这不会被诊断为 POSIXLY_CORRECT
的“无效”。
一般来说,检查外部实用程序的文档,了解它们在 POSIXLY_CORRECT
下的行为方式(如果他们关心的话)。 bash
手册只能告诉您 shell 及其内置命令如何受此环境变量的影响。
关于bash - 如何在 grep 中使用 POSIXLY_CORRECT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38605463/
有一个变量POSIXLY_CORRECT在 Bash 中 POSIXLY_CORRECT If this variable is in the environment when Bash starts
我是一名优秀的程序员,十分优秀!