如何在grep中使用POSIXLY_CORRECT? [英] How to use POSIXLY_CORRECT in grep?

查看:230
本文介绍了如何在grep中使用POSIXLY_CORRECT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bash中有一个变量 POSIXLY_CORRECT

There is a variable POSIXLY_CORRECT in Bash

POSIXLY_CORRECT

POSIXLY_CORRECT

如果Bash启动时该变量在环境中,则shell 在读取启动信息之前进入POSIX模式(请参阅Bash POSIX模式) 文件,就好像提供了--posix调用选项一样.如果是 在shell运行时设置,Bash启用POSIX模式,就像 命令

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

已被执行.

有人告诉我grep的某些选项不是POSIX,所以我在 GNU grep手册,发现:

I was told that some options of grep are not POSIX and so I confirmed in The Open Group Base Specifications Issue 6 for grep. So I checked the GNU grep manual and found:

grep 带有丰富的选项集:一些来自POSIX,一些是GNU扩展.长选项名称始终是GNU扩展, 即使对于POSIX规范中的选项也是如此.的选项是 由POSIX指定的名称,以它们的简称显式标记为 这样便于POSIX便携式编程.一些选项名称是 提供以与较旧或更旧的实现兼容.

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.

它还提到:

2.2环境变量

grep的行为受到以下环境变量的影响.

The behavior of grep is affected by the following environment variables.

POSIXLY_CORRECT
如果设置,则grep的行为与POSIX要求相同;否则,grep的行为将更类似于其他GNU程序. POSIX要求,紧随文件名的选项必须被视为文件名.默认情况下,此类选项被排列在操作数列表的最前面,并被视为选项.另外,POSIXLY_CORRECT禁用对无效括号表达式的特殊处理.参见invalid-bracket-expr.

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.

Using the part Long option names are always a GNU extension, even for options that are from POSIX specifications I said: let's try the variable POSIXLY_CORRECT against that.

所以我确实尝试了非POSIX的东西:

So I did try with something that is not POSIX:

$ echo "HELLO" | grep --ignore-case 'hello'
HELLO

但令我惊讶的是,它也可以设置它:

But to my surprise it also works setting it:

$ echo "HELLO" | POSIXLY_CORRECT=1 grep --ignore-case 'hello'
HELLO

我做错了什么?设置POSIXLY_CORRECT不能使grep无法识别长选项名称吗?

What am I doing wrong? Shouldn't a set POSIXLY_CORRECT make grep not recognize a long option name?

如果使用非POSIX的选项(例如-C),则会发生相同的情况:

The same occurs if using an option (for example -C) that is not POSIX:

$ POSIXLY_CORRECT=1 grep -C 2 '2' <<< "1
2
3"
1
2
3

与之前执行相同的所有操作set -o posix一样.

As well as doing all the same running set -o posix before.

推荐答案

摘自GNU grep手册:

POSIXLY_CORRECT

(如果已设置),grep的行为符合POSIX的要求;否则,grep 表现得更像其他GNU程序. POSIX要求 文件名后面的选项必须被视为文件名; 默认情况下,此类选项排列在 操作数列表,被视为选项.另外,POSIX 要求将无法识别的选项诊断为非法", 但由于它们并非真正违法,因此默认值为 诊断为无效". POSIXLY_CORRECT也禁用 _N_GNU_nonoption_argv_flags_,如下所述.

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.

这意味着在环境中为GNU grep设置POSIXLY_CORRECT的唯一作用是不允许重新排列在文件名之后出现的选项,以便将其放在最前面.它不会使它不带有非POSIX命令行标志.

This means that the only thing that setting POSIXLY_CORRECT in the environment does for GNU grep is that it's not allowed to rearrange options that occur after the filename so that the are placed at the front. It doesn't make it not take non-POSIX command line flags.

所以我们尝试一下:

$ ggrep "hello" myfile -v

$ env POSIXLY_CORRECT=1 ggrep "hello" myfile -v
ggrep: -v: No such file or directory

(在我的BSD系统上,GNU grep被称为ggrep)

(GNU grep is called ggrep on my BSD system)

手册中有关无法识别的选项"的部分是GNU grep默认情况下的工作,即在POSIXLY_CORRECT有无的情况下,-g标志都将被诊断为无效".由于--ignore-case是一个有效选项(虽然不是POSIX),但使用POSIXLY_CORRECT不能将其诊断为无效".

The part about "unrecognized options" in the manual is what GNU grep does by default, i.e. the -g flag will be diagnosed as "invalid" under both with POSIXLY_CORRECT and without. Since e.g. --ignore-case is a valid option (though not POSIX), this is not diagnosed as "invalid" with POSIXLY_CORRECT.

通常,请查看外部实用程序的文档,以了解它们在POSIXLY_CORRECT下的运行方式(如果有必要的话). bash手册只能告诉您此环境变量如何影响shell及其内置命令.

In general, check the documentation for external utilities for how they behave under POSIXLY_CORRECT (if they care at all). The bash manual can only tell you how the shell and its built-in commands are affected by this environment variable.

这篇关于如何在grep中使用POSIXLY_CORRECT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆